-
Notifications
You must be signed in to change notification settings - Fork 993
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
Positional arguments ignore "type": string"
#2359
Comments
That does apparently fail in quite a quiet way, but in fact there are some setup issues. Here is a working example with a few small changes:
const { argv } = yargs(["abc", "333"]) // subcommand + positional to test parsing
.scriptName("pirate-parser")
.usage("$0 [WCA event ID")
.command("abc <eventID>", "welcome ter yargs!", (yargs) => {
yargs.positional("eventID", {
type: "string",
describe: "WCA event ID",
});
return yargs;
})
.strictOptions(); % node index.js
{
_: [ 'abc' ],
'$0': 'pirate-parser',
eventID: '333',
'event-i-d': '333'
} |
The CLI I'm implementing doesn't have subcommands at the moment (which is quite common for commandline applications). What should I be doing in that case? |
Oh, I see! I seem to have ended up with some nested command code from debugging. Lemme see if I can take it out. |
Hmm, this seems to have the same issue: import yargs from "yargs";
const { argv } = yargs(["333"])
.scriptName("pirate-parser")
.usage("$0 [WCA event ID")
.positional("eventID", {
type: "string",
describe: "WCA event ID",
})
.strictOptions();
console.log(argv._[0] === "333"); // should be true?
console.log(argv._[0] === 333); // should be false?
console.log(argv.eventID); // or this should be defined? |
There is a trick. The positional gets stored using its name. import yargs from "yargs";
const { argv } = yargs(["333"])
.scriptName("pirate-parser")
.usage("$0 [eventID]", "welcome ter yargs", (yargs) => {
yargs.positional("eventID", {
type: "string",
describe: "WCA event ID",
})
})
.strictOptions();
console.log(argv); % node top.mjs
{ _: [], '$0': 'pirate-parser', eventID: '333', 'event-i-d': '333' } |
Ah, I see, thanks. I've used https://yargs.js.org/docs/#api-reference-positionalkey-opt states that " If I now understand that correctly, wouldn't it make more sense for the code to error when called like that? I'm a big fan of "if it compiles it works", and the current behaviour here is pretty unintuitive to me. |
Repro:
For example: https://runkit.com/embed/6o5nr4apu9v3
Expected:
argv._[0]
is a string. (Output:true
,false
.)Observed:
argv._[0]
is converted to a number. (Output:false
,true
.)The text was updated successfully, but these errors were encountered: