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

Aguments and Options together? #989

Closed
waynevanson opened this issue Jul 4, 2019 · 6 comments
Closed

Aguments and Options together? #989

waynevanson opened this issue Jul 4, 2019 · 6 comments

Comments

@waynevanson
Copy link

Hi Guys,

How do we provide flags for aguments? i don't mind what parts of the API to get there, I just want to get there. I read through the readme and the arguments section was the closest thing I could find.

In a CLI, it would be like @angular/cli

ng generate component [options] <pathToDirectory>

Where options are options flags, such as -c or -f.

What is best practice? Can options be added using .options into the arguments?

@shadowspawn
Copy link
Collaborator

You can add flags to commands.

const program = require("commander");

program
  .command("generate <pathToDirectory>")
  .description("description of generate")
  .option("-c,--compress", "description of compress")
  .action((path) => {
    console.log(`Path to directory is ${path}`);
  });

program.parse(process.argv);
$ node index.js --help
Usage: index [options] [command]

Options:
  -h, --help                            output usage information

Commands:
  generate [options] <pathToDirectory>  description of generate

$ node index.js generate --help
Usage: index generate [options] <pathToDirectory>

description of generate

Options:
  -c,--compress  description of compress
  -h, --help     output usage information

$ node index.js generate -c myPath
Path to directory is myPath

@waynevanson
Copy link
Author

waynevanson commented Jul 4, 2019

Sorry, closed by accident.

It does not quite resolve what I'm after. Looking to have the generate as a command, the component as a command/option and then any option flags between component and <pathToDirectory>

# current solution does this
ng generate component <pathToDirectory>
# but not this
ng generate component [options] <pathToDirectory>

@waynevanson
Copy link
Author

waynevanson commented Jul 4, 2019

After reading this about multiline cli's, it looks like the path of least resistance is to use commander to trigger another command program using the current one.

So ng generate would trigger the generate commander program, which could then have component [options] <path>

Have you seen this done before? Would there be a better way? Thank you for getting back to me so promptly before, I appreciate it.

@shadowspawn
Copy link
Collaborator

I'll give you a scatter of relevant information.

  1. Using .action to handle commands like in the example I gave, you can have one layer of subcommands. So as you noted, ng generate but not ng generate component.

  2. Using git style executables, you can have more layers.

  3. A handy feature for passing options to deeper commands you spawn yourself is that -- tells commander to stop looking for options and leave them alone. For example, I have a fab command which calls git. I can use it like this:

fab git -- checkout -b new-branch

(without the -- Commander would try and process the -b.)

  1. And lastly, some relevant open issues: how to use sub command with option? #521 Subcommands syntax and usage #764

@waynevanson
Copy link
Author

I think that maybe I'm after something a bit more customizable. It doesn't seem this or any other CLI tool that supports typescript properly exists to handle this easily. In example, i'd probably want an API that is a little more declarative:

import { Command } from 'commander'

const program = new Command()
.command('generate')
.command('component')
.options('flags', (option) => {
  option.add('-c', '--class', 'creates a class')
  option.add('-f', '--function', 'creates a function')
})
.parse(process.argv)

This way I know exactly what the user could input and in what order. I find the options in commander being at the start restrictive.

Thank you for your help again and apologies this doesn't support the mission of team Commander.

@shadowspawn
Copy link
Collaborator

Support for nested commands got added in Commander v5.0.0.

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