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

Prints whole usage string on incorrect arguments #32

Open
jpdoyle opened this issue Nov 2, 2018 · 1 comment · May be fixed by #59
Open

Prints whole usage string on incorrect arguments #32

jpdoyle opened this issue Nov 2, 2018 · 1 comment · May be fixed by #59
Assignees
Labels

Comments

@jpdoyle
Copy link

jpdoyle commented Nov 2, 2018

Consider this example.

Per the reference implementation, an incorrect sequence of arguments should output the brief usage description. I adapted the example from the readme:

{-# LANGUAGE QuasiQuotes #-}
module Main where
import System.Console.Docopt
import System.Environment (getArgs)

optparser :: Docopt
optparser = [docopt|
Naval Fate.

Usage:
  naval_fate.py ship new <name>...
  naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
  naval_fate.py ship shoot <x> <y>
  naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate.py -h | --help
  naval_fate.py --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.
|]

getArgOrExit = getArgOrExitWith optparser

main :: IO ()
main = do
  args <- parseArgsOrExit optparser =<< getArgs

  print args

However, when I execute it with no arguments I get:

Naval Fate.

Usage:
  naval_fate.py ship new <name>...
  naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
  naval_fate.py ship shoot <x> <y>
  naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate.py -h | --help
  naval_fate.py --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.

instead of the expected:

Usage:
  naval_fate.py ship new <name>...
  naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
  naval_fate.py ship shoot <x> <y>
  naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate.py -h | --help
  naval_fate.py --version
@FullstackGJJ FullstackGJJ self-assigned this Sep 25, 2021
@FullstackGJJ
Copy link
Collaborator

FullstackGJJ commented Sep 25, 2021

This is definitely a mismatch of behavior from what the original docopt intended. On incorrect arguments it should print only the usage section without the options section. The options section should only be included as an output if someone types --help in the example link.

jpdoyle added a commit to jpdoyle/docopt.hs that referenced this issue Sep 27, 2021
Fixes docopt#32.

This makes 3 main changes to this library's behavior:

1) `pUsagePatterns` (and thus `pDocopt`) parses out the "Usage:" section
   of the helpstring as a string and returns that. The way I'm parsing
   it feels really hacky but I wasn't sure a better way to achieve it.
2) `exitWithUsage` and `exitWithUsaegMessage` now print that "short
   usage string" rather than the whole helpstring. `exitWithHelpstring`
   has been added, which prints the whole helpstring.
3) `parseArgsOrExit` automatically exits printing the helpstring if
   `longOption "help"` is present, to be more consistent with the
   original `docopt` behavior. I'm not sure this is the correct place in
   the library to put it, but it creates the intended behavior.
@FullstackGJJ FullstackGJJ added this to the 0.7.0.8 release milestone Sep 29, 2021
@DigitalBrains1 DigitalBrains1 removed this from the 0.7.0.8 release milestone Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants