Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Making a CLI tool is a fulfilling thing.

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >

Share

Shulou(Shulou.com)11/24 Report--

This article comes from the official account of Wechat: low concurrency programming (ID:dibingfa). Author: flash.

I have asked for help in moments before, how to quickly make a CLI tool is a command-line tool, such as echo.

The great gods of the voting circle have contributed a lot of methods, so let me make a summary first.

For example, Spring Shell in the Java language can work with SpringBoot to create a command line tool, such as the following.

@ ShellComponent public class SSHCommand {@ ShellMethod (value = "connect to remote server") public void ssh (@ ShellOption (value = "- s") String host) {System.out.println (host);}} can support a ssh command with the-s argument.

Shell: ssh-s 192.168.0.3192.168.0.3 it is very convenient to use java to make such a small set of tools for yourself. Of course, if you don't want to use Spring, there are tools like JCommander that don't rely on the Spring suite, but the way you write it will be ugly.

In addition to the java language, the most recommended is the Go language Cobra library, the official website cobra.dev is very concise.

Specific we go to the official website to see, are very intuitive and clear demo, very comfortable. I haven't studied the rest in detail, so here's the feedback from the voting circle:

Dpdk

Click of python

Boost of C++

Rust clap

Nodejs

Java common-cli package

Urfave/cli

Xterm.js

Boost of cpp

But instead of mentioning the C language, one reader mentioned the getopt series of functions. What is this?

Regardless of what this function is, do you know who provides the commands you use, such as echo,cp,mv?

These belong to the coreutils utility class, such as cp-version, you can see the specific version information.

Since these CLI utility classes are all code in coreutils, let's take a look at the implementation here, which must be more elegant.

Open the source code of coreutils, find any command, such as basename.c to find its main method, you can see that it is the use of getopt_long to parse the command line parameters.

When it comes to the elegance of implementation, we have the same way as in coreutils, after all, there is nothing wrong with it.

But how exactly do you use getopt_long? When we man it, we can see a very detailed introduction, and here is a very simple demo that can be compiled and run directly.

By comparison, it is found that the structure of the usage in demo and coreutils is roughly the same, that is, the parameters of the getopt_long function are constantly called in the while loop, and then the value of the return value c is judged by switch to perform different operations.

At the same time, put the long_options-- parameter list in an array, using required_argument to indicate that parameters are required, and no_argument to indicate that parameters are not needed.

At this time, I have an idea that if I implement a set of coreutils, I can not only learn how to use C to make an elegant CLI tool, but also have an in-depth source understanding of commonly used shell commands. At the same time, we can also transform these commands to have our own characteristics, and lay the foundation for adding our own new commands in the future.

At the same time, at the bottom of many commands in coreutils, we also need to call the Linux system library, and we can also know more about some system library functions.

There are a lot of things at one stroke. Let's do it!

There is a particularly interesting and simple command in coreutils, yes. If you type yes on the Linux command line and press enter, you will find that it keeps outputting y on the command line, very fast, and that's the effect.

Can I implement one by myself and modify it so that it can output line numbers and control the time interval of the output, not so fast?

Just do it, and a dbf-yes tool has been made.

It can support the use of the-n parameter to represent the output line number, the-s parameter to indicate the number of seconds between time, and finally a parameter hehe to indicate what the character to output is.

Feel that this way of learning is still very good, involving a lot of knowledge, but also a great sense of achievement, like breaking through the customs to achieve all the tools in coreutils, to add their own characteristics.

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.

Share To

IT Information

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report