This article is first published in
The CSDN Article editor is too bad. We recommend that you use Cmd Markdown. It not only supports markdown, but also supports VIM mode.
Intelligent command line tool-betty
Betty tool command ruby
Betty is a simple and interesting intelligent command line prompt tool implemented in Ruby. The main problem solved is to translate English into unix commands that can be understood by machines. In its official documentation, We can find detailed instructions and explanations. The main purpose of this article is to customize our own "English commands" based on betty ".
Basic Principles
The main. rb file is the entrance of the entire program. Its main function is
- Use the command line parameters to load all executable files from lib/*. rb to find executable commands.
If the installation is successful, you will find ~ /. Bash_profile or another system configuration file contains a line of code similar to the following:
Alias betty = "xxx/betty/main. rb"
From the above analysis, we can see that betty is not responsible for specific command execution, but for converting our popular language (such as English) into commands that can be executed by the system.
File in lib/*. rb, responsible
- Determine whether the command matches (use regular match)
If yes, return the matching command and description in JSON format.
responses << {
:command => "grep --include=#{ pattern } -Rn #{ contains } #{ directory }",
:explanation => "Find files with extension that contains string."
Let's take betty find me all files that contain california as an example to explain the program running process.
The betty command maps to main. in the rb file, the main method is triggered to call the interpret method interpret. The main responsibility is to find matching commands in $ executors, the executor implemented by calling the interpret method in executor is the module defined in lib.
Customize your executor
Objectives:
To implement a prompt command, for example, enter betty give me a help and all command prompts will be displayed on the screen.
Based on the above analysis, we found that implementing our executor is very simple. The basic steps are as follows:
Define a new module in lib to define a class method interpret (command) if it matches the command, return response to register executor to $ executors
For specific code, see https://github.com/chucai/betty
Summary
Betty is really a fun tool, and it is very simple to implement your own specific commands. In addition, this project can develop in a more complex direction. For example, voice prompts similar to siri, and you can also customize your own Chinese prompts. Since this project was just started, there are still many shortcomings. For example, when we are processing a specific speech, we use regular expressions, which are not intelligent enough and the logic is very simple, if you want to customize your own requirements, you need to implement more details on your own. But in general, this is a great tool!