Picture, from the network

You usually work on Linux and write code, sometimes you need to write some command line parsing scripts. The argparse library is what we normally use. Today I went to Github and found a handy library called ‘Fire’. It’s an open source library from Google.

1. Traditional command line parsing, argparse library

Argparse is a library that is usually used when writing command line parsing. Argparse is a standard library, and argparse is very powerful, but the downside is that it can be a bit cumbersome to use, with arguments and so on.

1). Look at some very simple computing functions

For example, to write a very simple calculator script for adding, you need nine lines of code and a bunch of arguments to fill in.

Here is a very, very simple use of Argparse, but it is not easy for beginners to understand. There are too many arguments. Argparse has optional generation, positioning arguments. Generally divided into three steps:

  • First declare an ArgumentParser object, which adds a description of the program’s functionality

  • Then you declare a bunch of parsing rules, which is the most complicated part. Use the add_argments function, which has a bunch of arguments to fill in.

  • Finally, parse_args() is used to parse the incoming content.

If the script’s functionality is complex, the parsing area can be very extensive and very long. Is there a simple command line parsing library that is easy to use last time? Check out Google’s famous Fire library.

Google’s Fire library

The library has nearly 10,000 likes and 540 forks, which is pretty good! Why is it so cool? Let’s see how it’s used.

This is also the example above, adding two numbers.

It’s 6 lines of code to do all the above, and it’s very clean and refreshing! Is it very easy to understand? The usage is simple if we want to add related numbers:

python3 demo_fire.py add 10 20>>30Copy the code

Fire encapsulates all those annoying positioning arguments, optional arguments, and so on, and you don’t have to work too hard to become familiar with Argparse, just think about your logic. Save a lot of energy and time. If we want to extend the above script, add subtraction, multiplication and unless functionality! You simply add the function to the Calculator class.

>>

$ python3 demo_fire.py mul 10 2

20

$ python3 demo_fire.py sub 10 2

8

This is just the tip of the iceberg. There are many more interesting features. Such as command grouping function, access properties function. It’s time to ditch the Argparse library and embrace Fire. In fact, I took a closer look at some of the fire source code, which also calls argparse at the bottom of the library, but with a lot of wrapping to make it easier and simpler to use!

Link: https://github.com/google/python-fire/blob/master/docs/guide.md

Below recommend my small procedure, code problem master

Python circle first question with a prize applet

There are 50 new prizes for you to check

Code problem’s got talent

Fun · fun · many prizes

Long press · Scan code