leo / args Goto Github PK
View Code? Open in Web Editor NEWToolkit for building command line interfaces
Home Page: https://npmjs.com/args
License: MIT License
Toolkit for building command line interfaces
Home Page: https://npmjs.com/args
License: MIT License
My app has a config option called 'host'. When you run the program with "-h", the help is printed, which is not unexpected.
However, the following output is
const args = require('args')
.option('interval', 'How often (in seconds) to ping the device', 30, s => 1000 * s)
.option('delay', 'How long (in seconds) the device must be gone before reporting no presence', 15 * 60, s => 1000 * s)
.parse(process.argv)
Note that there are two '-h' options.
Hi!
So I have this simple script
var args = require('args');
var path = require('path');
var availableTypes = [
'all',
'products',
'conversion-chart'
];
args.option('type', 'The type of index to update ('+ availableTypes.join(', ') +')');
/*
args.options([
{ name: 'type', description: 'The type of index to update ('+ availableTypes.join(', ') +')' }
]);
*/
args.examples([
{ usage: path.basename(__filename) + ' update --type all', description: 'Update all search indexes.' },
{ usage: path.basename(__filename) + ' update --type products --type conversion-chart', description: 'Update only 2 specific search indexes.' }
]);
var flags = args.parse(process.argv, {
name: path.basename(__filename),
version: false
});
console.log(flags);
When I call the script scripts.js -t all
I get this
{ t: 'all', type: 'all' }
Now if I comment the args.option
and uncomment args.options
and run it again, I get this :
{ t: false, type: false }
shouldn't that give the same results or am I missing something?
Suggested by @rauchg: .parse(, { help: false, version: false })
The subcommand binary isn't found on windows environments
C:\> tool config get option
C:\Users\jduque\AppData\Roaming\npm\node_modules\tool\node_modules\args\index.js:342
throw err
^
Error: spawn tool.js-config ENOENT
i.e.
-c, --cache [value] Directory to cache stuffs (defaults to "/tmp/foo")
instead of (currently):
-c, --cache [value] Directory to cache stuffs
I can submit a PR if desired.
Devs should be able to specify these using .examples()
or something like that.
Run now -h to see what I'm talking about!
Some people don't like magic globals. So we should allow this (but still keep magic properties):
const properties = args.parse(process.argv)
This is a weird one. When passing a value to an argument via the command line, while also defining a fallback on process.env
, [object Object]
is returned instead of the correct value.
I took these steps to reproduce the issue:
npm install args
fail.js
with these contents:const args = require('args');
args.option(['p', 'port'], 'The port to run on', process.env.PORT || 3000);
const config = args.parse(process.argv);
console.log(`Your port is: ${config.port}`);
$ node fail.js
Your port is: 3000 # all good
$ node fail.js -p 1000
Your port is: 1000 # all good
$ PORT=1000 node fail.js
Your port is: 1000 # all good
$ PORT=1000 node fail.js -p 1001
Your port is: [object Object] # failure
Note if I take out process.env
in that script and instead specify a single default value of 3000
with no fallback everything works as expected.
Happy to look into resolving the issue if you don't have time to, as it's currently causing bugs in the master
branch of slackin here: rauchg/slackin#206
I think this syntax is more readable and easier to understand:
args
.option('a', 'abc')
.command('b', 'def')
.config({name: 'abcdef'})
args.parse(process.argv)
What do you think?
For example .colors({mainColor: 'red', subColor: 'bold' })
would output the commands
and options
in red, and the description in chalk.bold
instead of chalk.dim
If I run npm install release -g && release -v
, I get 3.0.1 - which is the latest version of args
.
π€ π€·ββοΈ
Hi! The new behavior of detecting unknown options is broken for subcommands. E.g.
$ foo bar -o=baz
The option "o" is unknown. Here's a list of all available options:
[etc]
In v2.4 (which I upgraded from) it works fine.
Since this property comes directly from minimist, we need to remove the paths of the binaries.
This will make it easy for devs to find out who created a CLI
My application accepts no commands, but has options. The setup looks like this
const args = require('args')
.option('interval', 'How often (in seconds) to ping the device', 30, s => 1000 * s)
.option('delay', 'How long (in seconds) the device must be gone before reporting no presence', 15 * 60, s => 1000 * s)
.parse(process.argv)
However, when I run the app with --help, the output looks like this
[chris:~/device-presence] master(+6/-1) Β± node index.js --help
Usage: index.js [options] [command]
Commands:
help Display help
Options:
-d, --delay <n> How long (in seconds) the device must be gone before reporting no presence
-h, --help Output usage information
-i, --interval <n> How often (in seconds) to ping the device
-v, --version Output the version number
Since my application does not use commands, it would be nice if the usage section did not imply that it did.
I want to specify a string option named "--version" but because of the default version option, it will not let me override it (as far as I can tell). I can submit a patch if this isn't supported.
Hey @leo,
This fix might not be necessary, but when I created my first args subcommand I got:
Error: spawn gest-test ENOENT
at exports._errnoException (util.js:1028:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
...
and addingpath.join(__dirname, this.binary + '-' + (Array.isArray(details.usage) ? details.usage[0] : details.usage))
was my quick fix, but this only fixed the problem if I named my binary gest-test
, not gest-test.js
. A better solution would be to only spawn the file mapped to by package.json
's bin
, or maybe default to .js
.
I can tackle this, but I would like to know which approach you think is best.
-- @mfix22
This will make managing the code and adding features in the future much easier!
0.10, 0.12
due to this
https://github.com/leo/args/blob/master/src/index.js#L312
for case when you have your command in folder like
script/
- test.js
- test-hello.js
with
//test
args.command('hello', 'say hello');
it will fail to find command with test.js-hello
.
const args = require('args')
args
.option('logs', 'Prints the build logs', true, Boolean)
console.error(args.parse(process.argv))
// { l: false, logs: false }
Expected false
to be true
.
Would a pull request be accepted that allows an argument to be false by default? Is there any way of doing this already?
This would be useful for implementing a command line switch to enable a specific function that is disabled by default.
Hi,
If there is already a short version with a given letter, I think next options starting with same letter should not expose a short version at all.
Here I'm trying to create a list PR and my --cors options conflits with the --cache option:
-c, --cache <n> How long static files should be cached in the browser (seconds)
-c, --cors <list> Setup * CORS headers to allow requests from any origin
As far as I understand, I can't setup no short version at all or a short version of more than 1 letter. It means there can't be more than 26 options available and I have to pick a random letter for my cors options
.showHelp()
workI really like this package and was wondering if you were planning on adding the ability to add information to the 'Usage' field?
For example:
Usage: haha [options] [command] <directory>
-h
is pretty cryptic and doesn't help newbs. Also having two ways for outputting the usage information is just confusing to everyone!
Since chalk
is being used in the main library it needs to be moved to dependencies
.
The showHelp will not console.log bigger helps due to the process.exit(). I think using process.exitCode or no exit at all would be better, and let the users to handle their exit on help.
thanks
Just like now
running now deploy
by default, we could add the ability to specify a default subcommand.
Maybe like this:
.defaultCommand('deploy', 'Deploy')
or
.command('deploy', 'Deploy', { defaultCommand: true }
or via the options
in .parse(process.argv, { defaultCommand: 'deploy' }
The script program
defines its option like this:
args.option('parameters', "Your parameters", []);
const flags = args.parse(process.argv);
console.log(flags);
A test in a terminal returns an empty array:
program command --parameters 10 20
Hi π Thanks for a great args parser.
What do you think of the possibility to send in an option that will stop the process.exit()
from the help generation? It can be useful in systems that has plugins which themselves has options. The snippet below shows an example use case:
const {adapter} = args
.option(['a', 'adapter'], 'The adapter used')
.parse(argv, { help: false, exit: false })
const options = args
.option(['a', 'adapter'], 'The adapter used')
.options(adapter.options)
.parse(argv, { name: `some-cli (with adapter "${adapter}")`)
I am happy to implement it if you think it would be an valuable to the library.
People should be able to show the help whenever they want to. Maybe rename to .showHelp()
Flags are options without a value... Might be a great idea to make this more explicit.
Makes promises fail loudly instead of silently (without .catch
). Might not make sense for submodules, but for CLIs, it's pretty great.
The only way to get to the values of the options shall be .parse(). For example, the current way of doing it makes option names conflict with existing methods of the module.
@ntwcklng We kinda forgot that people need to be able to specify an unknown sub command, as a path to a directory or similar:
serve directory
micro directory
Both of these would throw the help, because the "sub command" is unknown.
Of course we could just add the ability to deactivate this using a config parameter, but since these are like 90% of the cases, I would rather only use it for options, not sub commands. π π
since this one does not allow to pass 2nd argument opts
to minimist
https://github.com/leo/args/blob/master/src/index.js#L387
there is no way to make parser treat number looking values as strings like here https://github.com/substack/minimist#var-argv--parseargsargs-opts.
anyone?
Since defaults are used much more often than initializers, this...
.option(name, description, init, default)
...should be this...
.option(name, description, default, init)
When outputting the usage information, for example.
Currently, it looks like this (we need to remove [value]
):
-v, --version [value] Output the version number
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.