ndmitchell / cmdargs

Haskell library for command line argument processing
Other
91 stars 12 forks source link

cmdArgsRun on an invalid flag should say how to get help #34

Open ndmitchell opened 9 years ago

ndmitchell commented 9 years ago

From http://code.google.com/p/ndmitchell/issues/detail?id=418

Compare ghc (not cmdargs) with Hoogle (cmdargs):

$ ghc -h
ghc: unrecognised flags: -h

Usage: For basic information, try the `--help' option.

$ hoogle -h
Unknown flag: -h

Using cmdArgsRun, it should always output try the --help option (assuming the help option hasn't been renamed). Suggested by Nick Wu.

And an additional possible idea from bug #425 :

An alternative would be to define a variant of cmdArgsRun which interprets null getArgs to mean that it should print the help message (perhaps with a Bool argument to turn this on and off). Ideally, this variant would also allow other configurations like where to direct the printed output, etc, which could all be bundled together into a CmdArgsRunConfig argument.

@jfeltz remarked: This may be totally out of context, but with 0.10.2 I've gotten around this by calling Explicit.process directly:

import qualified System.Console.CmdArgs.Explicit as Exp                                                  

mode :: Mode (CmdArgs ProgramName)
mode = ...

main :: IO ()
main =
    env_args <- getArgs

    case (Exp.process mode env_args) of
      Right res ->
        case (cmdArgsHelp res) of
          Just h -> putStr h
          Nothing -> myHandleResultFunction $ cmdArgsValue res
      Left err  -> putStr $ err ++ "\n " ++ "(--help for assistance)\n"

This seems to work. Though I'm not sure what the full consequences/implications here are, I've just started using the library.

(Thanks @jfeltz for the suggestion, I had missed it on the Google bug tracker, but I've got it written down now for when I go through the GitHub tracker.)

ntc2 commented 6 years ago

I completely agree that typing an invalid flag should show help in addition to failing, but why not add -h to the list of default help flags as a step in the right direction? My first impulse after seeing Unknown flag: <flag> is to run the program again with -h. The current defaults are -? and --help, and I can't even type -? in ZSH without escaping it as -\?.