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
alias does not seem to work #2163
Comments
I have a couple of ideas on how to fix this: use positional arg (command)My personal opinions on this one:
const input = 'cmd1 abc'
yargs(input)
.command(
'cmd1 <foo>',
'cmd1 desc',
yargs => yargs
.positional('foo', { type: 'string' }),
argv => {
console.log({ argv })
})
.strict()
.parse() use positional arg (usage)Note: const input = 'abc'
yargs(input)
.usage(
'$0 <foo>',
'default desc',
yargs => yargs
.positional('foo', { type: 'string' }),
argv => {
console.log({ argv })
})
.strict()
.parse() usage with required flagconst input = '-f abc'
const argv = yargs(input)
.usage('Usage: $0 --foo THING [--bar THING2]')
.option('foo', { type: 'string', alias: 'f', demand: true })
.option('bar', { type: 'string', alias: 'b', demand: false })
.strict()
.parse()
console.log({ argv }) In the unit test, you can see that flags in the usage string are required, unless wrapped with I think your example is failing because |
Is I'll have to play around with this without the |
Yeah, |
I don't understand this line. Which value? |
For a required option using const input = 'cmd1 -f abc'
yargs(input)
.command(
'cmd1',
'cmd1 desc', // description could be 'Example: cmd1 -f "Hello World"'
yargs => yargs
.option('foo', { type: 'string', alias: 'f', required: true }),
argv => {
console.log({ argv })
})
.strict()
.parse() which would yield
|
I still can't get aliases to work properly without commands (or with them, since in my real-world case the options are "global" not per-command so I would expect something more like If you simply comment out the I played around a bit with your last example (comment just above this), and it seems like the big difference is my input (coming directly from When passing in a list, the alias does not work. But compare to passing in |
Here's an example with both global and command-specific options: yargs(process.argv.slice(2))
.command(
'$0',
'default desc',
yargs => yargs,
argv => console.log(argv)
)
.command(
'cmd1',
'cmd1 desc',
yargs => yargs
// chained onto command builder
.option('bar', { type: 'string', alias: 'b', required: true }),
argv => console.log(argv)
)
// chained globally
.option('foo', { type: 'string', alias: 'f', required: true })
.strict()
.parse() the output for
the output for
|
Passing a string or an array of strings to (There was a bug a while ago where extra sets of quotes were stripped differently between the two, but that should be fixed now) |
Like I said, it doesn't do that for me. I'm currently using node I've tried your example code as such: import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
yargs(hideBin(process.argv))
.command(
'$0',
'default desc',
(yargs) => yargs,
(argv) => console.log(argv),
)
.command(
'cmd1',
'cmd1 desc',
(yargs) =>
yargs
// chained onto command builder
.option('bar', { type: 'string', alias: 'b', required: true }),
(argv) => console.log(argv),
)
// chained globally
.option('foo', { type: 'string', alias: 'f', required: true })
.strict()
.parse(); When I invoke with
The same happens if I compile with Command-based required options also fail (e.g. via
|
good to know, I'll test it with Node 12 |
I could be wrong, but I think ts-node doesn't forward args, you might have to do something like this (Update: I re-read your comment and saw that you compiled with tsc and saw the same error) |
Can you test the following example without ts? I'm wondering if there is something in the compilation process that's messing things up. const input = process.argv.slice(2)
console.log({ input, version: process.version })
yargs(input)
.command(
'$0',
'default desc',
yargs => yargs,
argv => console.log(argv)
)
.command(
'cmd1',
'cmd1 desc',
yargs => yargs
.option('bar', { type: 'string', alias: 'b', required: true }),
argv => console.log(argv)
)
.option('foo', { type: 'string', alias: 'f', required: true })
.strict()
.parse() When I run
|
Similar results with node |
I have an example up on codesandbox. Can you verify that it works? I have no idea what could be causing this issue |
I wonder if this is an issue with typescript. I've been putting your examples into
I tested that outside of any sort of directory structure to make sure my existing |
If you're using a version of Node that supports esm, you can add If you can't modify your existing project like that, I suggest initializing a new project to test the code, or using the common js syntax (require). My suspicion is that the typescript compilation process is messing something up |
jly36963 was right with suspicion that TypeScript compilation is the problem. I recently discovered that the command-line flags appearing in the help is a big red flag that the TypeScript import is not working as intended. The short version is turn on |
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. |
Aliases appear to be completely ignored with
17.4.0
. Take this test script we'll callyargs.ts
:if I run it via
ts-node test.ts -e dev
, the output looks like:Not only is it reporting
env
as missing, but it seems to be adding---e
and--dev
to the list of options in the help text.I've tried this with node 12 (what I'm officially stuck on for a few more weeks) and node 14 (just to be sure), and with
yargs
16.x
and17.x
with the same results.Running
ts-node test.ts --env dev
works as expected and results in:The text was updated successfully, but these errors were encountered: