In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to quickly achieve command line prompts". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
A command line prompt
If the command line tool is a little more complex, you must provide a corresponding command line prompt, otherwise it is almost impossible for developers to use it. For example, Aliyun has a corresponding command line tool aliyun-cli [1]. After download and installation, you can use the aliyun command line tool. When you execute aliyun-help, you will find that there are a lot of subcommands. Without a command line tooltip, it is very complicated for developers to use this tool to check the documentation or enter commands through the help on the command line.
Aliyun's command line tool also provides corresponding code hints, as follows:
This command line prompt is not bad, you just need to select the corresponding subcommand and then prompt it.
Most developers like command line prompts with descriptions. Not all subcommands and command parameters are well named, such as the live subcommand prompt given by the aliyun command line, you may not know what this live is (of course, as a classmate of Aliyun, I still know that live is a live video broadcast). Command line prompts like the following description are much more intuitive:
Generate command line prompts
The mechanism of command line prompts for various shell, such as bash,zsh,fish, is not introduced here. No one will write these command line prompt scripts manually, and everyone will use the framework to generate command line prompt scripts corresponding to shell.
I found some command line parsing frameworks, and can automatically generate command line prompts, such as Java's picocli,Node.js 's commander.js,Python 's argparse, and Rust's clap-rs. I tried it all, and finally found that the command line prompt generated by clap-rs is better, which is the kind of description I said, and there are automatic prompts for file names and directories, prompts for enumerated values, etc., the key is also very simple. If any students have a better command line parsing framework, I hope you can leave a message and share it.
So how can command-line tools written in other languages, such as Node.js,Java,Python, achieve the same effect as command-line prompts in clap-rs?
Command line YAML file of three clap-rs
Clap-rs contains a YAML specification for command-line tools. We all know that command-line tools are easy to interact with, mainly in two parts: parameters and subcommands. You can see that things like-- conf xxx.yaml with parameter names are parameters, or you can omit the parameter names, such as convert a.jpg a.png, where a.jpg and a.png are also parameters. Subcommands are easier to understand. The git we use every day uses a lot of subcommands, such as git add xxx.jpg. While subcommands can continue to apply subcommands, subcommands also have their own parameters.
Based on the characteristics of the command line, we can completely describe the usage specification of command-line tools through YAML, and now everything can be YAML.
Here I give an YAML definition of the Aliyun command line tool, which is, of course, just demo. As follows:
Name: aliyun2 version: "0.1.0" about: "cli for Alibaba Cloud" args:-version: short: v long: version takes_value: false about: Display version subcommands:-oss: about: object Storage subcommands:-cat: about: cat text file args:-file: Takes_value: true required: true about: file name-ls: about: list file-ecs: about: CVM subcommands:-SendFile: about: send file-AddTags: about: add tags
As you can see, I first defined two subcommands: oss and ecs, and then under the OS subcommand I defined two more subcommands: cat and ls. For the cat subcommand of oss, I added the parameter file so that I can use cat to view the contents of the text file on oss.
With this command line tool YAML specification definition, I can call the command line tool interface provided by clap-rs to generate the corresponding prompt script for shell. The effect is as follows:
Is this command line prompt much better than the original one? With a description of the prompt, it is much easier to select subcommands and parameters.
Fourth, write YAML for all command-line tools
At this point, I believe we all understand. Whether the tool is written by Java,Python,Node.js or Rust, first define the YAML specification of the tool, and then the developer writes code according to the specification, he can choose his favorite language, his favorite command line parser, and then implement the corresponding function. Without code improvement, it is very difficult to write YAML files without errors, so I made a JSON Schema [2] file, which can be prompted for code when writing YAML files, making it easier to write command-line YAML specification files. The usage of JSON Schema is as follows:
Next, based on the YAML file, we will generate corresponding command line prompt scripts for various shell, such as bash,zsh,fish and powershell, so that after separation, the developer does not need to deal with command line prompts that he does not know, or find the corresponding SDK of the programming language to do command line code prompts. What if not? Even if there is, what if the generated prompt is very simple? After all, command line tooltips are very important.
I believe that developers of Node.js do not want to learn about Rust and clap-rs, which is too inefficient. So I wrote another tool, cli-completion [3], whose main purpose is to help you automatically generate command line prompt scripts for various shell according to the YAML file mentioned above. Take a look at the example of zsh:
$cli-completion-- zsh commands/aliyun2.yaml > / usr/local/share/zsh/site-functions/_aliyun2 $autoload-U compinit & & compinit
Take another look at the example of oh-my-zsh:
$mkdir ~ / .oh-my-zsh/custom/plugins/aliyun2 $cli-completion-- zsh aliyun2.yaml > ~ / .oh-my-zsh/custom/plugins/aliyun2/_aliyun2
In this way, cli-completion can provide command line prompts for any command. In other words, in the future, all you have to do is write command-line logic and leave all the questions about command-line prompts to cli-completion to help you generate them. Of course, considering the user experience, you may need to quickly synchronize the scripts generated by cli-completion to the client environment through a subcommand in the command line tool.
The development process of the command line: the YAML specification is written, the command line prompt is generated automatically, and the developer completes the function before leaving work.
Some students may ask whether I can automatically generate the skeleton of the whole application based on the YAML file and combined with a command line parsing framework. Developers only need to implement some functions, and the development will be easier. Personally, I don't think it's a problem to use these frameworks to generate code automatically.
Fifth, make cli-completion FaaS.
This function may not be used twice a year, and it is troublesome to install it. FaaS is not everywhere now, we can give it a try. First of all, cli-completion is written in Rust, so you can write Rust Cloud Lambda in the traditional way, and then deploy it to cloud services. In addition, you can write a Rust Web application, such as actix-web, which is very simple.
None of this is fashionable. We intend to WebAssembly the code of cli-completion and deploy it as FaaS. Here I choose CloudFlare as the running platform for FaaS. Let's take a look at Demo.
Create a cli.yaml file as follows:
Name: cli1 version: "0.1.0" about: "CLI completion for bash, zsh, fish and powershell." Args:-help: short: h long: help takes_value: false about: Display this help
Then call cli-completion 's FaaS service, and you can get the corresponding command line prompt script code. The command is as follows:
Curl-H 'Content-Type: application/x-yaml'-- data-binary "@ cli.yaml" https://cli-completion.linux-china.workers.dev/completion/zsh
Compared with traditional cloud lambda or cloud function, FaaS has the fastest response speed, and the number of service invocations is very small, basically, each request is cold start, while WebAssembly has a great advantage in this respect.
Of course, there is also one of the biggest reasons: WebAssembly-style FaaS, which is the cheapest.
Beside the question, let's discuss the implementation of cloudflare's WebAssmebly, pure technical discussion, the code is as follows:
Async function handleRequest (request) {const {greet} = wasm_bindgen await wasm_bindgen (wasm) const greeting = greet () return new Response (greeting, {status: 200})}
In the above code, wasm is a WebAssembly.Module object that is injected from the outside, not written by the developer, but generated by FaaS. The next step is to get the export function of wasm from the function wasm_bindgen, then call wasm_bindgen (wasm) to associate the greet function with the export function in wasm module, and then call greet to go to the call to wasm module. If this is the case, WebAssembly.Module can actually be managed externally, and when there is a request, it is associated with the function of JavaScript, which ensures the fast response of WebAssembly.
This is the end of the content of "how to quickly implement command line prompts". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.