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

global options (common options) - program.parse() deleting command help #1739

Closed
ChipLuxury-EWA opened this issue May 28, 2022 · 7 comments
Closed

Comments

@ChipLuxury-EWA
Copy link

ChipLuxury-EWA commented May 28, 2022

Hello and many thanks for commander!
This is my first issue applying (:

it seems that when I parsing the global options the global help is run over the command help.
expected behaviour: global help will add to the command help.

also notice that this work without 'process.argv' like in the docs example.

my code:

program
    .name("Weather App V2")
    .description("The best weather app!")
    .version("1.0.1")
    .option(
        "-s, --scale <string>",
        "choose c for celsius or f for fahrenheit (you can also k for kelvin if you know what you doing.)",
        "c" //default value
    );

program.parse(); //this line insert the help and options from 'name' to 'command'

program
    .command("get-temp")
    .description("Get the temperature by city name.")
    .argument("<string>", "city name")
    .option("-t, --test", "does help print me when parsing?", false)
    .action((city) => {
        const units = setUnitsForDegree(program.opts().scale);
        getTempByCityName(city, apiKey, units);
    });
2022-05-28.23-05-23.mp4
@shadowspawn
Copy link
Collaborator

shadowspawn commented May 28, 2022

Hi! You should call .parse() just once, after configuring the whole program.

In your example, when you specify -h on the command-line the code after this line is not reached, the program displays the top-level help and exits.

program.parse(); //this line insert the help and options from 'name' to 'command'

@ChipLuxury-EWA
Copy link
Author

ChipLuxury-EWA commented May 29, 2022

But then when I'm calling program.parse() at the end I don't see the global help:
node weather.js get-temp -h

Usage: Weather App V2 get-temp [options] <string>

Get the temperature by city name.

Arguments:
  string      city name

Options:
  -t, --test  check me (default: true)
  -h, --help  display help for command

Maybe the help could be something like this:

Usage: Weather App V2 get-temp [options] <string>

Get the temperature by city name.

Arguments:
  string      city name

Global Options:
  -s, --scale <string>            choose c for celsius or f for fahrenheit (you can also k for kelvin if you know what you
                                  doing.) (default: "c")

Command Options:
  -t, --test  check me (default: true)
  -h, --help  display help for command

Thanks!

@shadowspawn
Copy link
Collaborator

When you call node weather.js get-temp -h

  • node weather.js: this is the program
  • get-temp: this is the subcommand

To see the program help with the global options:

node weather.js -h

There isn't currently a way to show the global options along with the subcommand.

@shadowspawn
Copy link
Collaborator

Suppose you set things up so you can type weather to launch the application, like:

weather get-temp -h

Then weather is the name of the program, so you would have:

program
    .name("weather")

@ChipLuxury-EWA
Copy link
Author

ChipLuxury-EWA commented May 29, 2022

OK THANKS!
my workaround:

const commonOptionsMessage =
    "\nCommon Options:\n\t-s, --scale <string>\
    \n\tchoose c for celsius or f for fahrenheit (you can also k for kelvin if you know what you doing.)\
    \n\t(default: 'c')\n";

program
    .command("get-temp")
    .description("Get the temperature by city name.")
    .argument("<string>", "city name")
    .option(commonOptionsMessage)
    .action((city) => {
        ........
    });

program
    .command("get-detailed-forecast")
    .description("Get more then just a temperature, get a detailed weather forecast!")
    .argument("<string>", "city name")
    .option(commonOptionsMessage)
    .action((city) => {
        .......
    });

Then when using command help:
node weather.js get-temp -h

Usage: Weather App V2 get-temp [options] <string>

Get the temperature by city name.

Arguments:
  string                        city name

Options:
  
  Common Options:
  -s, --scale <string>      choose c for celsius or f for fahrenheit (you can also k for kelvin if you know what you doing.)    
                                        (default: 'c')
  
  -h, --help                    display help for command

@shadowspawn
Copy link
Collaborator

Have a look at .addHelpText() to do the same sort of thing in a supported way.

@shadowspawn
Copy link
Collaborator

An answer was provided, and no further activity in a month. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

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

2 participants