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

Specific error message #54

Closed
amigalemming opened this issue Oct 26, 2013 · 8 comments
Closed

Specific error message #54

amigalemming opened this issue Oct 26, 2013 · 8 comments

Comments

@amigalemming
Copy link

When I enter a wrong argument the example program responds with the general usage pattern:

$ example --hello
Usage: args --hello TARGET [--quiet]

An error message like "missing parameter for option 'hello'" would be more helpful.
Or "option X required", "unknown option", "option parameter must be integer" etc.

@pcapriotti
Copy link
Owner

"Unknown option" errors should work already. If you have an example where they don't, please open a separate ticket.

Some of the other errors can be manually specified by using a custom reader and the noArgError modifier, e.g.:

nullOption (  long "hello"
           <> metavar "TARGET"
           <> reader (\s -> msum
                [ auto s
                , readerError "expected int" ])
           <> noArgError (ErrorMsg "missing int parameter") )

but I do agree that the defaults should be more informative.

@amigalemming
Copy link
Author

On Sat, 26 Oct 2013, Paolo Capriotti wrote:

"Unknown option" errors should work already. If you have an example
where they don't, please open a separate ticket.

I am refering to the example given in the docs:
http://hackage.haskell.org/package/optparse-applicative-0.7.0.2

@pcapriotti
Copy link
Owner

When running the example, I get:

$ ./sample --foo
Invalid option `--foo'

Usage: sample --hello TARGET [--quiet]

Am I missing something?

@amigalemming
Copy link
Author

On Sat, 26 Oct 2013, Paolo Capriotti wrote:

When running the example, I get:

$ ./sample --foo
Invalid option `--foo'

Usage: sample --hello TARGET [--quiet]

Am I missing something?

I get the same result for this example. However, I don't want the general
"Usage" message, if there is a specific error.

Also I tried "--hello" without an argument and I tried "--quiet" without
"--hello" and got no specific error message in both cases.

@pcapriotti
Copy link
Owner

Why not? If the user specified an invalid option, it is likely that they are confused about the available options, and the "usage" message can be helpful. I don't think it hurts either way.

I guess I could add a preference to never display the usage message when a more specific error can be displayed, but I'd rather limit the number of such preferences.

With the changes in the formatting branch, it should be relatively easy to use execPureParser and then do the error reporting yourself, if you're not happy with how execParser does it.

@amigalemming
Copy link
Author

On Sat, 26 Oct 2013, Paolo Capriotti wrote:

Why not? If the user specified an invalid option, it is likely that they
are confused about the available options, and the "usage" message can be
helpful. I don't think it hurts either way.

In my experience it means that I have just mistyped an option or forgot
something. The Usage message can become rather long and would distract me
from the real error message. If it is a more complex problem I may have to
make more attempts. In this case the terminal is quickly filled with the
repeating "Usage" message that has no value anymore after the first time.
Last but not least, most shell commands only report the general usage
message if asked to do so.

With the changes in the formatting branch, it should be relatively easy
to use execPureParser and then do the error reporting yourself, if
you're not happy with how execParser does it.

ok

@amigalemming
Copy link
Author

github has hidden my additional comment:

On Sat, 26 Oct 2013, amigalemming wrote:

Also I tried "--hello" without an argument and I tried "--quiet" without
"--hello" and got no specific error message in both cases.

@amigalemming
Copy link
Author

next attempt:

Also I tried "--hello" without an argument and I tried "--quiet" without
"--hello" and got no specific error message in both cases.

pcapriotti added a commit that referenced this issue Aug 27, 2014
It should now be relatively easy to customise the appearance of the generated
help text.

For example, in #54, @amigalemming asks for a way to get rid of the usage line
when a specific error message is displayed.  This is now easy to achieve:

    exec :: ParserPrefs -> ParserInfo a -> IO a
    exec pprefs pinfo = do
      res <- execParserPure pprefs pinfo <$> getArgs
      let f h | isEmpty (helpError h) = h
              | otherwise = h { helpUsage = mempty }
      handleParseResult (overFailure f res)
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