我正在构建一个brainfuck 编译器。可执行文件接受两个命令 $ brainfuck compile ...
和 $ brainfuck run
.我希望可执行文件在按 Tab 键时自动完成。例如。写作 $ brainfuck com
然后按 Tab 应该生成 $ brainfuck compile
.
data Command = Compile CompileArgs | Run RunArgs
deriving (Show)
main :: IO ()
main = execute =<< execParser opts
where
opts = info (helper <*> argsParser) fullDesc
execute :: Command -> IO ()
execute (Compile args) = compile args
execute (Run args) = run args
argsParser :: Parser Command
argsParser = subparser (compileCommand <> runCommand)
where
compileCommand = command "compile" $ info compileOptions $ progDesc "Compile brainfuck to an executable"
runCommand = command "run" $ info runOptions $ progDesc "Execute brainfuck code"
optparse 的 github 页面上有一个部分 here ,但我真的不明白。
函数
completeWith :: Options.Applicative.Builder.Internal.HasCompleter f => [String] -> Mod f a
看起来很像command :: String -> ParserInfo a -> Mod CommandFields a
我已经在使用了。所以我想我可以使用它并将它们与 <>
结合起来但事实证明 CommandFields
不是 HasCompleter
的实例.你应该如何让自动完成工作?
最佳答案
在 RTFM 之后,我发现了如何配置自动完成。completeWith
在为各个参数构建解析器时应用。
像这样:
data CompileArgs = CompileArgs
{
debug :: Bool,
optimizations :: OptimizationLevel,
file :: String
}
deriving (Show, Read)
compileArgsParser :: Parser CompileArgs
compileArgsParser = CompileArgs
<$> switch (
long "debug" <>
help "Outputs object and assembly files")
<*> option auto (
long "optimization-level" <>
value All <>
metavar "LEVEL" <>
help "all | none, default: all" <>
completeWith ["all", "none"])
<*> argument str (
metavar "FILE" <>
help "brainfuck source code" <>
action "file")
<**> helper
action
是关于如何自动完成的指令。 "file"
意味着自动完成任何文件或目录。见 this页面了解更多信息。为了让这些自动完成启动,您需要生成一个脚本并确保该脚本是源代码。按照惯例,它放在
/etc/bash_completion.d/
下使用 bash 时。brainfuck --bash-completion-script `which brainfuck` | sudo tee /etc/bash_completion.d/brainfuck
在我的情况下,我的程序被称为
brainfuck
.
关于haskell - optparse-applicative bash 自动补全如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59117975/