Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flags with optional values #61

Open
JuanM04 opened this issue Apr 4, 2022 · 2 comments
Open

Flags with optional values #61

JuanM04 opened this issue Apr 4, 2022 · 2 comments

Comments

@JuanM04
Copy link

JuanM04 commented Apr 4, 2022

Is there a way to have optional flag values? Something like the following:

const arg = require('arg');

const args = arg({
	'--port': Number, // --port <number>
	'--host': String.optional, // --host [string]
});

Given --port 5432 --host localhost it should return { _: [], port: 5432, host: 'localhost' },
given --port 5432 --host it should return something like { _: [], port: 5432, host: true },
and given --port 5432 it should return { _: [], port: 5432 },

@cspotcode
Copy link

I'd like this for ts-node to emulate the behavior of node's -p flag. node -p 123 is equivalent to node -pe 123

@psychobolt
Copy link

psychobolt commented Dec 28, 2023

Workaround is to pass the permissive flag and use a string type, then find the arg value manually:

// cmd.js
const defaultSince = 'origin/main';

const { _ = [], ..args } = arg({
  '--since': defaultSince
}, { permissive: true });

console.log(_);

const sinceIndex = _.indexOf('--since');
if (sinceIndex > -1) {
  console.log(_[sinceIndex + 1]);
}
node cmd.js --since develop

# [ '--since', 'develop' ]
# develop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants