Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

haskell - optparse应用bash自动补全如何工作?(How does optparse-applicative bash autocompletion work?)

I'm building a brainfuck compiler.

(我正在构建一个令人难以置信的编译器。)

The executable accepts two commands $ brainfuck compile ... and $ brainfuck run .

(该可执行文件接受两个命令$ brainfuck compile ...$ brainfuck run 。)

I want the executable to auto complete when pressing tab.

(我希望可执行文件在按Tab键时自动完成。)

Eg writing $ brainfuck com and then pressing tab should generate $ brainfuck compile .

(例如,编写$ 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"

There is a section on optparse's github page here , but I don't really understand it.

(此处有关于optparse的github页面的部分,但我不太了解。)

The function completeWith :: Options.Applicative.Builder.Internal.HasCompleter f => [String] -> Mod fa looks quite similar to command :: String -> ParserInfo a -> Mod CommandFields a which I'm already using.

(函数completeWith :: Options.Applicative.Builder.Internal.HasCompleter f => [String] -> Mod fa看上去与我已经在使用的command :: String -> ParserInfo a -> Mod CommandFields a非常相似。)

So I figured I could use it and just combine them with <> but it turns out that CommandFields is not an instance of HasCompleter .

(所以我想我可以使用它,并将它们与<>结合使用,但事实证明CommandFields不是HasCompleter的实例。)

How are you supposed to get the auto completion to work?

(您应该如何使自动完成工作?)

  ask by The Hoff translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have not tested this, but after reading the documentation, it seems to me that by calling execParser in main , your program automatically supports the required options for command complete.

(我没有对此进行测试,但是在阅读了文档之后,在我看来,通过在main调用execParser ,您的程序会自动支持命令完成所需的选项。)

You just need to run your program with --bash-completion-script as documented to generate a shell script, and then load that script to bash.

(您只需要使用记录的--bash-completion-script运行程序来生成shell脚本,然后将该脚本加载到bash即可。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...