Giter Club home page Giter Club logo

Comments (7)

pcapriotti avatar pcapriotti commented on July 18, 2024

It is certainly possible. Use customExecParser (or any of the other functions in Extra that take a ParserPrefs) to run your option parser, and give it a ParserPrefs parameter containing showHelpOnError. For example:

import Options.Applicative

opts :: Parser Int
opts = option (  short 'n'
              <> metavar "THREADS"
              <> help "Set the number of threads" )

main :: IO ()
main = do
  r <- customExecParser
         (prefs showHelpOnError)
         (info (opts <**> helper) idm)
  print r

from optparse-applicative.

wdanilo avatar wdanilo commented on July 18, 2024

Hi! Thank you for your response. I'm afraid this is not exactly what I want. I do not want to print help on error. I want print it only when there are no arguments provided. When error occurs I want to display only a message (like it is done by default).

from optparse-applicative.

pcapriotti avatar pcapriotti commented on July 18, 2024

I'm afraid the only way to get this behaviour is to create your own version of customExecParser. You can find it in Options.Applicative.Extra. I'll leave this ticket open as a remainder to make customExecParser a bit more modular, so that you wouldn't need to copy-paste as much to get a custom behaviour.

from optparse-applicative.

spl avatar spl commented on July 18, 2024

+1 I was also looking for a way to print help when a command is called with no arguments or flags.

from optparse-applicative.

jcristovao avatar jcristovao commented on July 18, 2024

Hi! I'm currently considering one other use case, that I believe may be related (if not let me know, I'll post this as a new issue): would it be possible to define a default command? Lets say my program supports two commands, load and save, each receives specific options. But if the user did not specify either command, I would like to assume it is a load command, as it is almost always the case.

The same principle could be considered for help, I guess.

I've tried adding an empty command <> command "" (info parseLoadOpts ..., but then I must explicitly do:
program "" filetoload

Thanks

from optparse-applicative.

pcapriotti avatar pcapriotti commented on July 18, 2024

@jcristovao I think you can just duplicate the options of your default command outside subparser, then run the parser with noBacktrack, see #31. Please do open another issue if that doesn't work for you or if I misunderstood your problem.

from optparse-applicative.

jml avatar jml commented on July 18, 2024

#100 was marked as a duplicate of this, but I don't understand how to make a version of customExecParser that prints the help output.

from optparse-applicative.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.