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

Allow options to be used before commands #25

Closed
balupton opened this issue Sep 6, 2018 · 6 comments
Closed

Allow options to be used before commands #25

balupton opened this issue Sep 6, 2018 · 6 comments

Comments

@balupton
Copy link
Contributor

balupton commented Sep 6, 2018

I am looking to implement cac into DocPad. However, I need to be able to specify options before or after commands.

Here is an example source file:

'use strict'

const cli = require('cac')()

cli.option('opt', {
	type: 'boolean',
	desc: 'a boolean option'
})

cli.command('cmd', {
	desc: 'a command'
}, function (input, flags) {
	console.log('cmd:', { input, flags })
})

cli.command('*', {
	desc: 'show help'
}, function (input, flags) {
	cli.showHelp()
})

cli.parse()

And here is its output:

> node cac.js --opt cmd

  cac.js 6.81.0

  project description

  USAGE

    cac.js <command> [options]

  COMMANDS

    cmd  a command
    *    show help

  GLOBAL OPTIONS

    -v, --version  Display version                     [Type: boolean]
    -h, --help     Display help (You're already here)  [Type: boolean]
    --opt          a boolean option                    [Type: boolean]
> node cac.js cmd --opt
cmd: { input: [],
  flags:
   { version: false,
     v: false,
     help: false,
     h: false,
     opt: true,
     '--': [] } }

The former output should behave the same as the latter output.

@balupton
Copy link
Contributor Author

balupton commented Sep 6, 2018

Ok figured out a workaround.

For DocPad's use case, there is a --global option that for historical reasons needs to be in any position. With the workaround, we just check for --global manually, if it exists then we trim it from process.argv (to not annoy cac) as well as do our logic (in our case it is a if statement match, to skip the alternative if statements)

https://github.com/docpad/docpad/blob/3a8e77625bbeb0bcd334676fbbeab58603297a76/source/lib/cli.coffee#L8-L12

@egoist
Copy link
Collaborator

egoist commented Sep 7, 2018

If they work in the same way then bin --opt cmd would be very confusing, you can't tell if it is a sub command unless you already know --opt's type is boolean

BTW Can you list some software that actually allow this? In git you can do git log --log-size but git --log-size log doesn't work.

@balupton
Copy link
Contributor Author

balupton commented Sep 8, 2018

unless you already know --opt's type is boolean

Which is information cac does know

BTW Can you list some software that actually allow this?

npm

> npm --global uninstall serve
removed 69 packages in 0.46s
> serve
fish: Unknown command 'serve'
> npm --global install serve
/usr/local/bin/serve -> /usr/local/lib/node_modules/serve/bin/serve.js
+ serve@10.0.0
added 69 packages from 36 contributors in 5.491s
> serve

   ┌──────────────────────────────────────────────────┐
   │                                                  │
   │   Serving!                                       │
   │                                                  │
   │   - Local:            http://localhost:5000      │
   │   - On Your Network:  http://192.168.1.24:5000   │
   │                                                  │
   │   Copied local address to clipboard!             │
   │                                                  │
   └──────────────────────────────────────────────────┘

^C
INFO: Gracefully shutting down. Please wait...

@balupton
Copy link
Contributor Author

Going to close this for now, as I have no need for this feature anymore, as the workaround I came up with earlier is working fine for my use case.

It would be nice to see, as other CLI apps do use this, but it is suitable edge case that has workarounds that it isn't important.

@egoist
Copy link
Collaborator

egoist commented Nov 24, 2018

So, I think CAC will support this in 6.0 🤔 Working on it!

@jgoux
Copy link

jgoux commented Feb 2, 2023

Hello, I encountered this issue as well.

A good use case is shebang! I'm working on making vite-node compatible with shebang and I realized that :
#!/usr/bin/env vite-node --script-mode was not giving me what I expect.

When calling the script with something like: snaplet-scripts dev, process.argv will be:

[
    '/Users/jgoux/Library/pnpm/nodejs/18.13.0/bin/node',
    '/Users/jgoux/Documents/code/snaplet-labs/node_modules/.bin/vite-node',
    '--script-mode',
    '/Users/jgoux/Documents/code/snaplet-labs/apps/api/node_modules/.bin/snaplet-scripts',
    'dev'
  ]

And cac will interpret it like this:

files: [ 'dev' ],
  options: {
    '--': [],
    scriptMode: '/Users/jgoux/Documents/code/snaplet-labs/apps/api/node_modules/.bin/snaplet-scripts'
  }

Ideally I would expect:

files: [ '/Users/jgoux/Documents/code/snaplet-labs/apps/api/node_modules/.bin/snaplet-scripts', 'dev' ],
  options: {
    '--': [],
    scriptMode: true
  }

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

3 participants