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

Single hyphen/dash argument for indicating stdin #434

Closed
moll opened this issue Aug 7, 2021 · 5 comments
Closed

Single hyphen/dash argument for indicating stdin #434

moll opened this issue Aug 7, 2021 · 5 comments

Comments

@moll
Copy link

moll commented Aug 7, 2021

Hey

Thanks for you continued maintenance of Optparse Applicative!

I'm having trouble finding a way to specify a single - as an expected value, a pattern used to refer to reading from standard input. I'd like the help text to show (-|<path>) and the parsing to result in either, let's say, Nothing given - on the command line and Just path :: Maybe FilePath if a path other than - was given. Is that possible at the level of Optparse?

In other words, I'm looking for something to replace with:

(Opt.flag' Nothing (…) <|>
(Just <$> Opt.argument Opt.str (Opt.metavar "<path>")))

This should then permit invoking an app with ./foo - to read from stdin and ./foo file to read from file, with the --help text nicely differentiating the two use-cases.

Thanks in advance!

@moll
Copy link
Author

moll commented Aug 7, 2021

I'm thinking, if there were a mechanism for specifying expected constants, one could use that to both expect a single dash - argument and even support other advanced syntax in the style of ./foo rename <from> to <target>.

I noticed eitherReader. Maybe that combined with argument and metavar could be used to fake what I'm going for...

@moll
Copy link
Author

moll commented Aug 7, 2021

For now, I ended up going with:

data PathArg path = StdinArg | PathArg path

Opt.argument
  (Opt.maybeReader parsePathArg)
  (mconcat [Opt.metavar "(-|<csv-path>)"])

parsePathArg "-" = Just StdinArg
parsePathArg path = Just (PathArg path)

@HuwCampbell
Copy link
Collaborator

That's more or less what I would have suggested.

@HuwCampbell
Copy link
Collaborator

There's a solution to this which you've figured out for yourself, but obviously this could be better documented.

I'll add a page to the wiki for it when I get some time.

@moll
Copy link
Author

moll commented Aug 16, 2021

Thanks! And a built-in constant parser may be useful in the future, too, as my solution is rather verbose.

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

2 participants