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

Number as option alias not working as advertised #1454

Closed
madflow opened this issue Oct 16, 2019 · 11 comments
Closed

Number as option alias not working as advertised #1454

madflow opened this issue Oct 16, 2019 · 11 comments
Labels

Comments

@madflow
Copy link

madflow commented Oct 16, 2019

Hello!

When using a number as alias - it is advertised in the help section as -1 - but this does not work. (The option stays undefined).

// run.js
const argv = require('yargs')
  .option('negativeone', {
    alias: '1',
    type: 'boolean',
    description: 'Negative one'
  }).argv;

console.log(argv);
➜  yargs-test node run.js --help
Optionen:
  --help             Hilfe anzeigen                                    [boolean]
  --version          Version anzeigen                                  [boolean]
  --negativeone, -1  Negative one                                      [boolean]

➜ yargs-test node ./run.js --negativeone
{ '1': true, _: [], negativeone: true, '$0': 'run.js' }

➜ yargs-test node ./run.js -1
{ _: [ -1 ], '$0': 'run.js' }

➜ yargs-test node ./run.js --1
{ '1': true, _: [], negativeone: true, '$0': 'run.js' }

@mleguen
Copy link
Member

mleguen commented Nov 27, 2019

@madflow Did you try setting the parse-numbers option to false?

yargs.parserConfiguration({
  "parse-numbers": false
})

@mleguen
Copy link
Member

mleguen commented Nov 27, 2019

No, it does not work either.

@bcoe
Copy link
Member

bcoe commented Nov 27, 2019

do we want to support numbers as options, is a question worth asking ... this feels a little weird.

@madflow
Copy link
Author

madflow commented Nov 29, 2019

do we want to support numbers as options, is a question worth asking ... this feels a little weird.

@bcoe I am not sure at whom this question is directed - but since I logged this issue: It is weird and felt nonintuitive in our "downstream" application. We removed the number alias.

@mleguen
Copy link
Member

mleguen commented Nov 29, 2019

@bcoe I also fell its weird (but maybe some people use it?)

However, if we decide we do support numbers as options, shouldn'd we explicitely not support it (ie throw when a number is passed as a key or an alias, and stop parsing --1 as { '1': true } in yargs-parser)?

@benjie
Copy link

benjie commented Nov 30, 2019

I don't think numeric CLI flags are weird; quite a few of the standard unixy tools use numeric CLI flags. I'm sure many of us have used kill -9 from time to time, but here's a few others off the top of my head:

grep

$ grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE.
[...]
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM

gzip

$ gzip --help
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).
[...]
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better

ssh

$ ssh 
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [...]

rsync

$ rsync --help
rsync  version 3.1.2  protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
[...]
     --noatime               do not alter atime when opening source files
 -4, --ipv4                  prefer IPv4
 -6, --ipv6                  prefer IPv6

xargs

$ xargs --help
Usage: xargs [OPTION]... COMMAND [INITIAL-ARGS]...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.

Mandatory and optional arguments to long options are also
mandatory or optional for the corresponding short option.
  -0, --null                   items are separated by a null, not whitespace;
                                 disables quote and backslash processing and
                                 logical EOF processing

@bcoe
Copy link
Member

bcoe commented Nov 30, 2019

@benjie see #1493, we thought of a couple other examples as well.

@bcoe
Copy link
Member

bcoe commented Nov 30, 2019

Is it enough that we document the option as --1 (CC: @mleguen); I don't think we necessarily want to extend the underlying parser to treat [0 - 9] as flag arguments, I see the argument for:

--9
--100

@benjie
Copy link

benjie commented Dec 4, 2019

Is there a precedent for --9 etc options in other tools?

@bcoe
Copy link
Member

bcoe commented Dec 5, 2019

@benjie the tool I had in mind was node-tap, which has a --100.

@mleguen
Copy link
Member

mleguen commented Dec 11, 2019

Closed by #1493

@mleguen mleguen closed this as completed Dec 11, 2019
ruyadorno added a commit to ruyadorno/yargs-parser that referenced this issue Mar 7, 2020
As discussed on yargs/yargs#1454
even though there are plenty of valid usecases for single-digit
aliases and many well stablished examples, it is still
completely unsupported on yargs-parser.

This changeset introduces the possibility of using this simple numeric
alias in the context of boolean flags.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants