module Futhark.CodeGen.Backends.GenericPython.Options
( Option (..)
, OptionArgument (..)
, generateOptionParser
)
where
import Futhark.CodeGen.Backends.GenericPython.AST
data Option = Option { optionLongName :: String
, optionShortName :: Maybe Char
, optionArgument :: OptionArgument
, optionAction :: [PyStmt]
}
data OptionArgument = NoArgument
| RequiredArgument
| OptionalArgument
generateOptionParser :: [Option] -> [PyStmt]
generateOptionParser options =
[Assign (Var "parser")
(Call (Var "argparse.ArgumentParser")
[ArgKeyword "description" $
String "A compiled Futhark program."])] ++
map parseOption options ++
[Assign (Var "parser_result") $
Call (Var "vars") [Arg $ Call (Var "parser.parse_args") [Arg $ Var "sys.argv[1:]"]]] ++
map executeOption options
where parseOption option =
Exp $ Call (Var "parser.add_argument") $
map (Arg . String) name_args ++ argument_args
where name_args = maybe id ((:) . ('-':) . (:[])) (optionShortName option)
["--" ++ optionLongName option]
argument_args = case optionArgument option of
RequiredArgument ->
[ArgKeyword "action" (String "append"),
ArgKeyword "default" $ List []]
NoArgument ->
[ArgKeyword "action" (String "append_const"),
ArgKeyword "default" $ List [],
ArgKeyword "const" None]
OptionalArgument ->
[ArgKeyword "action" (String "append"),
ArgKeyword "default" $ List [],
ArgKeyword "nargs" $ String "?"]
executeOption option =
For "optarg" (Index (Var "parser_result") $
IdxExp $ String $ fieldName option) $
optionAction option
fieldName = map escape . optionLongName
where escape '-' = '_'
escape c = c