Comments (5)
Commander has built-in support for --
to indicate the end of the options, and any remaining arguments will be used without being interpreted.
Alternatively, you may be able to use .passThroughOptions()
so you don't even need to use the --
. In your example, this is similar to how npx is working.
https://github.com/tj/commander.js#parsing-configuration
from commander.js.
If you use .passThroughOptions()
then the user does not need to use the --
. The arguments after the first one are automatically passed through as normal command-arguments. Try:
./index.js file-name --value 10
from commander.js.
The options before the first argument are parsed as normal, so you would call like:
// with passThroughOptions
./index.js --silent filename --value 10
But using --
is fine too! And may be clearer if the syntax of your command does not make it feel natural which option belongs to the program and which gets passed through.
from commander.js.
Oh yeah, I see! That is fantastic. Do you think it would be nice to separate "the rest" of options from the original list? Looks like currently I get all the args altogether (including --
) - so I have to manually look for --
and trim array.
program
.name('run-script')
.version(version)
.description('run arbitrary unsafe JS scripts for fun')
.argument('<filename>', 'script to run')
.passThroughOptions()
// If I do `./index.js file-name -- --value 10`
// I get the following:
program.args: [ 'file-name', '--', '--value', '10' ]
program.opts: {}
// my goal is to run file-name and pass options (--value 19) there
// like so: require('child_process').exec(`node file-name --value 10`, ...)
// or so: require(...).spawn('node', ['file-name', '--value', '10'], ...)
from commander.js.
I see, awesome! Only thing is how would I differentiate between my args (if --value is for me) and pass-through options if they come after the command? If I have a flag --silent
to run that script without attaching it to stdout/stderr, I won't be able to run this like ./index.js filename --silent
? I would expect something like ./index.js filename --silent -- --value 10
to make sense.
I mean I understand .passThroughOptions()
and I can achieve the behavior I want, but it kinda looks easier to just do --
and pass everything else unvalidated to some kind of program.rest
. Anyway, this is a suggestion and the issue is answered, so I will close it shortly. Thank you for your patience and responsiveness
from commander.js.
Related Issues (20)
- [Bug] Unexpected arguments ignored HOT 2
- Commander v12 breaks implicit negative flag. HOT 1
- Dev: purging stale release branches HOT 1
- 115 HOT 1
- Add support for option to output object HOT 8
- Handle the case when node called with the "-e" argument HOT 4
- Compatible with deno HOT 2
- Neutral JavaScript runtime support (Deno, Bun, etc) HOT 11
- Allow option to conflict with the negation of another option. HOT 5
- When use .command("dtsgen <files>")๏ผthis params files in macOS and Windows is errors caused by differences HOT 3
- Requiring via `<` does not work. HOT 4
- how to use the ESM module to import and export ? HOT 2
- Option.fullDescription() does not exist HOT 2
- Is it possible for an option to conflict with an argument? HOT 5
- Usage with Bun HOT 5
- Action not exiting when using nodejs streams HOT 3
- error: unknown command with a -e flag (bun) HOT 6
- Help option disappears when configuring visible options HOT 4
- Short option name with single character or multiple characters have different result HOT 2
- "error: unknown command" when using addCommand
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commander.js.