GithubHelp home page GithubHelp logo

Double Dash Feature about commander.js HOT 5 CLOSED

lxchurbakov avatar lxchurbakov commented on June 14, 2024
Double Dash Feature

from commander.js.

Comments (5)

shadowspawn avatar shadowspawn commented on June 14, 2024 1

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.

shadowspawn avatar shadowspawn commented on June 14, 2024 1

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.

shadowspawn avatar shadowspawn commented on June 14, 2024 1

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.

lxchurbakov avatar lxchurbakov commented on June 14, 2024

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.

lxchurbakov avatar lxchurbakov commented on June 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.