You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to use a single dash as an option, specifically to mark the use of stdin.
ie echo 123 | cat -
Reacted by Soheil Rashidi, Allxie Cleary, Daniel Lenksjö, Matthieu Puibaraud, Richard Caceres, Helio Machado, Daniel D. Beck, Frederik Ring, Juri Leino, Caesar Schinas and 4 more
Reacted by Allxie Cleary, Helio Machado and Juri Leino
I found other useful hints on stdin and would rather not use var anywhere nowadays. So I came up with something along the lines of:
(yargs)=>{constinputFile=(yargs.file==='-') ? process.stdin.fd : yargs.file;// do something with filename, maybe returning it here is the best option// const fileContents = fs.readFileSync(inputFile, 'utf-8');returninputFile}
I ended up doing the replacecement (- by standard input) in the coerce functions.
But, in general, I'd say it's pretty important to allow parsing - as CLI input. Which don't seem able to do at present.
E.g., given a command spec like
const typecheckCmd = {
command: 'foo <input>',
desc: 'Check types (TBD) and effects of a TNT specification. Reads from stdin if <input> is -.',
handler: fooHandler,
}
When I run cmd foo -, the value for yargs.input ends up being true.
I know node util.parseArgs and Minimist and Commander all have special case processing to support single dash as an option and argument value. And I think in all three cases it got added after the initial implementation!
Since the reason for special treatment of arguments starting with dashes is for options, and a single dash is not an option, it makes sense to allow it once you know about the convention.
You can currently use - as an option value or as a positional value for a subcommand by explicitly setting nargs to 1. (It also happens to work without configuration for short options.)
I still think the single dash could be supported by default, but exploring what is possible now.
// positional.jsconstyargs=require('yargs/yargs')constresult=yargs(process.argv.slice(2)).command('run <input>','run the server',(yargs)=>{yargs.nargs('input',1);}).command('exec <input>','run the server',(yargs)=>{}).parse();console.log(result);
Sorry for digging up an old issue. I needed to do just this today and discovered a somewhat unexpected and maybe hacky way of implementing this. In my case I'm expecting an array of things, but I would assume this can be modified to work with single arguments as well.
This is written in TypeScript.
importyargsfrom'yargs'import{readFileSync}from'fs'functiongetStdinValues(){// If stdin is a TTY ie. it's the user typing then we're don't want to read// from stdin as it's most likely not there.if(process.stdin.isTTY){return[]}// Read from stdin and split the input by lines.conststdin=readFileSync(process.stdin.fd,'utf-8')returnstdin.trim().split('\n').map(line=>line.trim()).filter(line=>line.length>0)}typeOptions={things: string[]}yargs.scriptName('my-script').command<Options>('$0 <things...>','do something with the things',()=>{},(argv)=>{// Destruct argv so that we don't modify it.let{ things }=argv// It appears that if "things" is mandatory but the user passes a single// dash (-) it still passes validation but the array is empty. So this is// a nice albeit hacky indicator for us to read from stdin instead.if(things.length===0){things=getStdinValues()if(things.length===0){console.error('No things supplied on stdin')process.exit(1)}}// "things" is guaranteed to be a non-empty arrayconsole.log(things)}).parse()
Activity
rchrd2 commentedon Oct 22, 2020
Unfortunately, even inputting
-as a string, eg--file="-"produces""as the value ofargv.file.rchrd2 commentedon Oct 22, 2020
@ALMaclaine Why did you close it? Is it fixed?
ALMaclaine commentedon Oct 23, 2020
Lol because I thought you were a dev and that was a response on why it couldn't happen. Sorry.
bcoe commentedon Oct 27, 2020
@ALMaclaine @rchrd2 this isn't a convention I'm accustomed to. Can you point me to some tools that document using
-to indicatestdin.ALMaclaine commentedon Oct 27, 2020
@bcoe https://unix.stackexchange.com/questions/16357/usage-of-dash-in-place-of-a-filename
I however, don't remember why I wanted it, I think someone else asked me to implement it in replace.
rchrd2 commentedon Oct 27, 2020
Here's an example in python:
Source: https://stackoverflow.com/a/59381303
Also:
For utilities that use operands to represent files to be opened for either reading or writing, the '-' operand should be used to mean only standard input (or standard output when it is clear from context that an output file is being specified) or a file named -.(https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html)I'm not sure where the convention originated from, but it is widely used.
buske commentedon Oct 12, 2021
Was just trying to do this and found a solution mentioned here: https://livebook.manning.com/book/node-js-in-action-second-edition/chapter-11/9
It seems like the solution is for the
--fileoption to have nargs = 1 to ensure the-gets parsed as a value rather than another option. For example:line-o commentedon Feb 15, 2022
@buske your comment helped me lot.
I found other useful hints on stdin and would rather not use
varanywhere nowadays. So I came up with something along the lines of:I ended up doing the replacecement (
-by standard input) in the coerce functions.shonfeder commentedon Dec 5, 2022
This is a relatively widely used convention in linux/unix world: https://www.baeldung.com/linux/dash-in-command-line-parameters
But, in general, I'd say it's pretty important to allow parsing
-as CLI input. Which don't seem able to do at present.E.g., given a command spec like
When I run
cmd foo -, the value foryargs.inputends up beingtrue.This is on
[email protected]shadowspawn commentedon Feb 25, 2023
I know node util.parseArgs and Minimist and Commander all have special case processing to support single dash as an option and argument value. And I think in all three cases it got added after the initial implementation!
Since the reason for special treatment of arguments starting with dashes is for options, and a single dash is not an option, it makes sense to allow it once you know about the convention.
shadowspawn commentedon Mar 4, 2023
You can currently use
-as an option value or as a positional value for a subcommand by explicitly settingnargsto 1. (It also happens to work without configuration for short options.)I still think the single dash could be supported by default, but exploring what is possible now.
shadowspawn commentedon Mar 4, 2023
No accident,
yarns-parserhas tests for-for short option and positional, might just be missing the long option support: https://github.com/yargs/yargs-parser/blob/3aba24ceaa1a06ceb982c63a06002526d781e826/test/yargs-parser.cjs#L1489shadowspawn commentedon Mar 14, 2023
I think this will be fixed by yargs/yargs-parser#472
scriptcoded commentedon Feb 24, 2024
Sorry for digging up an old issue. I needed to do just this today and discovered a somewhat unexpected and maybe hacky way of implementing this. In my case I'm expecting an array of things, but I would assume this can be modified to work with single arguments as well.
This is written in TypeScript.
shadowspawn commentedon Oct 10, 2026
Merged yargs/yargs-parser#472 into
23.x.xbranch of yargs-parser. Have not tested behaviour in yargs yet.fix(cli): pass positional values starting with `-` through to tools
fix(cli): pass positional values starting with `-` through to tools
fix(cli): pass positional values starting with `-` through to tools
fix(cli): pass positional values starting with `-` through to tools
fix(cli): pass positional values starting with `-` through to tools
fix(cli): pass positional values starting with `-` through to tools (C…