ndmitchell / cmdargs

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

Unable to make args mandatory #21

Closed toothbrush closed 9 years ago

toothbrush commented 9 years ago

Hello! Great library, thank you. I'm struggling though, and i feel like i'm doing what the docs say i should do. I'm using version cmdargs-0.10.12.

So i have this bit of code:

data DiaspecCompiler = Pretty { inFile :: FilePath
                              , outFile :: Maybe FilePath}
                     | Racket { inFile :: FilePath
                              , outFile :: Maybe FilePath}
                     | Java   { inFile :: FilePath }
                     deriving (Show, Data, Typeable)

pretty = Pretty { inFile = def &= args &= typFile
                , outFile = Nothing &= typFile} 

java   = Java   { inFile = def &= args &= typFile }

racket = Racket { inFile = def &= args &= typFile
                , outFile = Nothing &= typFile} 

mode = cmdArgsMode $ modes [racket, java, pretty]

main :: IO ()
main = do
  opt <- cmdArgsRun mode

When i run ./mything --help i get the following output, though:

The diaspeccompiler program

diaspeccompiler [COMMAND] ... [OPTIONS]

Common flags:
  -? --help          Display help message
  -V --version       Print version information

diaspeccompiler racket [OPTIONS] [FILE]

  -o --outfile=FILE

diaspeccompiler java [OPTIONS]

  -i --infile=ITEM 

diaspeccompiler pretty [OPTIONS]

  -i --infile=ITEM 
  -o --outfile=ITEM

Which looks like nonsense to me. I expect each mode to have a mandatory FILE argument at the end, since all modes must operate on an input file. Where is the -i option coming from? Am i doing something brain-dead?

ndmitchell commented 9 years ago

Try setting {-# OPTIONS_GHC -fno-cse #-} at the top of the file - does that fix it?

toothbrush commented 9 years ago

That's pretty cool, it seems better now, thanks! I'll get back to you if my more complex case also fails (the one i'm working towards).

ndmitchell commented 9 years ago

This issue tends to crop up more with small test cases than with real examples, but the same fix should work. Please let me know if anything else causes you problems.

dan-t commented 9 years ago

Hi Neil,

I think, that I got a pretty similar problem:

data Args =
   ...
   | Dump { output     :: String
          , cabalFiles :: [FilePath]
          }
   deriving (Data, Typeable, Show, Eq)

dumpArgs :: Args
dumpArgs = Dump
   { output     = def &= explicit &= typ "FILE" &= name "output" &= name "o"
                      &= help "Save libraries with lower bounds to file, if empty, then it's written to stdout."
   , cabalFiles = def &= args &= typ "CABAL_FILE"
   }

 $> cabal-bounds dump --help
 cabal-bounds dump [OPTIONS]

 Flags:
   -o --output=ITEM            Save libraries with lower bounds to file, if
                               empty, then it's written to stdout.
   -c --cabalfiles=CABAL_FILE
 Common flags:
   -h --help                   Display help message
   -v --version                Print version information
      --numeric-version        Print just the version number

The problem is, that I've no idea when this broke, because it certainly worked at the beginning.

Yes, adding {-# OPTIONS_GHC -fno-cse #-} solves the issue, but it certainly doesn't feel like the Haskell way.

Greetings, Daniel