Improve command-line option parsing

This commit is contained in:
Joscha 2019-11-19 11:45:24 +00:00
parent 0d18329354
commit 75e102ed7d
3 changed files with 51 additions and 53 deletions

View file

@ -46,7 +46,7 @@ settingsParser = Settings
<> metavar "OUTFILE" <> metavar "OUTFILE"
<> help "If specified, write the memory dump to this file after execution is finished") <> help "If specified, write the memory dump to this file after execution is finished")
<*> switchWithNo "discover" True <*> switchWithNo "discover" True
(help "Disable the automatic loading of the .mima-flags and .mima-symbols files") "Try to load .mima-flags and .mima-symbols corresponding to the .mima input file"
<*> (optional . strOption) <*> (optional . strOption)
(long "flag-file" (long "flag-file"
<> short 'f' <> short 'f'
@ -59,11 +59,12 @@ settingsParser = Settings
<> help "A file containing label names and addresses, specified in the .mima-symbols format") <> help "A file containing label names and addresses, specified in the .mima-symbols format")
<*> (optional . option auto) <*> (optional . option auto)
(long "steps" (long "steps"
<> short 'n'
<> metavar "N" <> metavar "N"
<> help "How many instructions to execute (if not specified, runs until HALT or execution exception)") <> help "How many instructions to execute (if not specified, runs until HALT or execution exception)")
<*> flag False True <*> flag False True
(long "norun" (long "no-run"
<> help "Don't run the MiMa. Use the initial state for all further actions. Roughly equivalent to -n 0") <> help "Don't run the MiMa. Use the initial state for all further actions. Roughly equivalent to --steps 0")
<*> flag False True <*> flag False True
(long "quiet" (long "quiet"
<> short 'q' <> short 'q'
@ -71,7 +72,7 @@ settingsParser = Settings
<*> formatConfigParser <*> formatConfigParser
opts :: ParserInfo Settings opts :: ParserInfo Settings
opts = info (helper <*> settingsParser) $ fullDesc <> failureCode 1 <> footer formatConfigHelp opts = info (helper <*> settingsParser) $ fullDesc <> failureCode 1 <> footer flagFooter
{- Loading the flag file -} {- Loading the flag file -}

View file

@ -2,7 +2,6 @@
module Mima.Format.State module Mima.Format.State
( FormatConfig(..) ( FormatConfig(..)
, defaultFormatConfig
, FormatEnv(..) , FormatEnv(..)
, FormatReader , FormatReader
, Formatter , Formatter
@ -43,21 +42,6 @@ data FormatConfig = FormatConfig
, fcShowLabels :: Bool -- Currently unused , fcShowLabels :: Bool -- Currently unused
} deriving (Show) } deriving (Show)
defaultFormatConfig :: FormatConfig
defaultFormatConfig = FormatConfig
{ fcSparse = True
, fcShowRegisterFlags = True
, fcShowMemoryFlags = True
, fcShowAddressDec = True
, fcShowAddressHex = True
, fcShowAddressBin = False
, fcShowWordDec = True
, fcShowWordHex = True
, fcShowWordBin = False
, fcShowInstructions = True
, fcShowLabels = False
}
data FormatEnv = FormatEnv data FormatEnv = FormatEnv
{ feState :: MimaState { feState :: MimaState
, feFlags :: Flags (MimaAddress -> Bool) , feFlags :: Flags (MimaAddress -> Bool)

View file

@ -1,6 +1,7 @@
module Mima.Options module Mima.Options
( switchWithNo ( flagFooter
, formatConfigHelp , switchWithNo
, hiddenSwitchWithNo
, formatConfigParser , formatConfigParser
) where ) where
@ -8,38 +9,50 @@ import Options.Applicative
import Mima.Format.State import Mima.Format.State
switchWithNo :: String -> Bool -> Mod FlagFields Bool -> Parser Bool flagFooter :: String
switchWithNo name defaultValue fields flagFooter = "To disable an option, prepend 'no-' to its name (e. g. to disable"
| defaultValue = flag' False noMod <|> flag True True yesMod ++ " '--discover', use '--no-discover'). This only applies to options"
| otherwise = flag' True yesMod <|> flag False False noMod ++ " with a default of 'enabled' or 'disabled'."
where
yesMod = long name <> hidden <> fields
noMod = long ("no-" ++ name) <> hidden
formatConfigHelp :: String enabledOrDisabled :: Bool -> String
formatConfigHelp = "All options labeled with 'Formatting:' can be negated by prepending 'no-' to their name (e. g. '--sparse' becomes '--no-sparse')." enabledOrDisabled False = "disabled"
enabledOrDisabled True = "enabled"
switchWithNo :: String -> Bool -> String -> Parser Bool
switchWithNo name defaultValue helpText =
flag' False noMod <|> flag defaultValue True yesMod
where
noMod = long ("no-" ++ name) <> hidden
yesMod = long name <> help (helpText ++ " (default: " ++ enabledOrDisabled defaultValue ++ ")")
hiddenSwitchWithNo :: String -> Bool -> String -> Parser Bool
hiddenSwitchWithNo name defaultValue helpText =
flag' False noMod <|> flag defaultValue True yesMod
where
noMod = long ("no-" ++ name) <> hidden
yesMod = long name <> hidden <> help (helpText ++ " (default: " ++ enabledOrDisabled defaultValue ++ ")")
formatConfigParser :: Parser FormatConfig formatConfigParser :: Parser FormatConfig
formatConfigParser = FormatConfig formatConfigParser = FormatConfig
<$> switchWithNo "sparse" False <$> hiddenSwitchWithNo "sparse" True
(help "Formatting: Omit uninteresting addresses") "Omit uninteresting addresses"
<*> switchWithNo "register-flags" True <*> hiddenSwitchWithNo "register-flags" True
(help "Formatting: For each address, show all the memory flags that are active for that address") "For each address, show all the memory flags that are active for that address"
<*> switchWithNo "memory-flags" True <*> hiddenSwitchWithNo "memory-flags" True
(help "Formatting: For each address, show all registers currently pointing to that address") "For each address, show all registers currently pointing to that address"
<*> switchWithNo "address-dec" True <*> hiddenSwitchWithNo "address-dec" True
(help "Formatting: Display addresses in decimal") "Display addresses in decimal"
<*> switchWithNo "address-hex" True <*> hiddenSwitchWithNo "address-hex" True
(help "Formatting: Display addresses in hexadecimal") "Display addresses in hexadecimal"
<*> switchWithNo "address-bin" False <*> hiddenSwitchWithNo "address-bin" False
(help "Formatting: Display addresses in binary") "Display addresses in binary"
<*> switchWithNo "word-dec" True <*> hiddenSwitchWithNo "word-dec" True
(help "Formatting: Display words in decimal") "Display words in decimal"
<*> switchWithNo "word-hex" True <*> hiddenSwitchWithNo "word-hex" True
(help "Formatting: Display words in hexadecimal") "Display words in hexadecimal"
<*> switchWithNo "word-bin" False <*> hiddenSwitchWithNo "word-bin" False
(help "Formatting: Display words in binary") "Display words in binary"
<*> switchWithNo "instructions" True <*> hiddenSwitchWithNo "instructions" True
(help "Formatting: Show instructions") "Show instructions"
<*> switchWithNo "labels" True <*> hiddenSwitchWithNo "labels" True
(help "Formatting: Show labels from the symbol file") "Show labels from the symbol file"