Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a required flag using System.Console.CmdArgs.Implicit?

Tags:

haskell

Documentation clearly mention the use 'def', which provides a default value (empty) for the flag

sample = Sample{hello = def &= help "World argument" &= opt "world"}
         &= summary "Sample v1"

but I couldn't find a way to mention that I want to force the flag to be required.

Should I really go for Explicit or is there any way to define a required flag in Implicit?

like image 984
Christian Lemer Avatar asked May 07 '13 04:05

Christian Lemer


1 Answers

The documentation for opt states that

Note that all flags in CmdArgs are optional, and if omitted will use their default value.

So it seems that it's not possible to define a flag as mandatory using the implicit style.

You can, however, detect the omission of a flag by using a Maybe value. E.g.

data Sample = Sample { hello :: Maybe String }
sample = Sample{hello = def &= help "World argument" &= opt "world"}
     &= summary "Sample v1"

Now if the flag is omitted, you get a Nothing and if the user provides a value, you get Just "value". This means that you can check for Nothing yourself and terminate with an appropriate error message.

like image 92
shang Avatar answered Nov 11 '22 18:11

shang