Two lines of code to generate a command line for a Python script

Posted by williejoe on Wed, 05 Jan 2022 17:36:39 +0100

Sometimes we have such a demand:

We define a Python method. The method receives some parameters, but we want to expose these parameters on the command line when calling.

For example, here is a crawling method:

import requests

def scrape(url, timeout=10):
    response = requests.get(url, timeout=timeout)
    print(response.text)

Here, a scene method is defined. The first parameter receives the url, that is, the crawled url, and the second parameter receives the timeout, that is, the specified timeout.

When calling, we may call as follows:

scrape('https:///www.baidu.com', 10)

If we want to change the parameters and url, we have to change the code, right.

So sometimes we want to expose these parameters on the command line. At this time, we may use argparse and other libraries to declare the purpose of each parameter one by one. It is very cumbersome. The code is as follows:

parser = argparse.ArgumentParser(description='Scrape Function')
parser.add_argument('url', type=str,
                    help='an integer for the accumulator')
parser.add_argument('timeout',  type=int,
                    help='sum the integers (default: find the max)')

if __name__ == '__main__':
    args = parser.parse_args()
    scrape(args.url, args.timeout)

In this way, we can smoothly use the command line to call this script:

python3 main.py https://www.baidu.com 10

Does it feel very troublesome? argparse is smelly and long. It's hard to think about it.

Fire

But next, we'll introduce a library. With it, we only need two lines of code to do the above operation.

This library is called Fire. It can quickly add command-line parameter support for a Python method or class.

Take a look at the installation method first, and use pip3 to install:

pip3 install fire

So we can install it.

use

Let's look at a few examples.

Method support

The first code example is as follows:

import fire

def hello(name="World"):
  return "Hello %s!" % name

if __name__ == '__main__':
  fire.Fire(hello)

Here we define a hello method, then receive a name parameter, the default value is World, and then output the string Hello plus name.

Then we import the fire library, call its fire method and pass in the hello method declaration. What will happen?

We save this code as Demo1 Py, and then run it with Python 3:

python3 demo1.py

The operation results are as follows:

Hello World!

It doesn't look different.

But if we run the following command at this time, we can see some magical things:

python3 demo1.py --help

The operation results are as follows:

NAME
    demo1.py

SYNOPSIS
    demo1.py <flags>

FLAGS
    --name=NAME
        Default: 'World'

As you can see, here it converts the name parameter into an optional parameter on the command line. We can replace the name parameter with - name +.

Let's try:

python3 demo1.py --name 123

Here, we pass in a name parameter of 123. At this time, we find that the running result becomes the following:

Hello 123!

Is it very convenient? We can easily support and replace command line parameters without argparse.

What if we cancel the default value of the name parameter? Rewrite the code as follows:

import fire

def hello(name):
  return "Hello %s!" % name

if __name__ == '__main__':
  fire.Fire(hello)

Run again at this time:

python3 demo1.py --help

You can see that the results are as follows:

NAME
    demo1.py

SYNOPSIS
    demo1.py NAME

POSITIONAL ARGUMENTS
    NAME

NOTES
    You can also use flags syntax for POSITIONAL ARGUMENTS

At this time, we find that the parameter name becomes a mandatory parameter. We must specify the parameter content in the command line, and the call will become the following command:

python3 demo1.py 123

The result is the same.

Class support

Of course, the fire library supports not only adding command line support to methods, but also adding command line support to a class.

Let's take another example:

import fire

class Calculator(object):    
    def double(self, number):
        return 2 * number

if __name__ == '__main__':
    fire.Fire(Calculator)

We save this code as demo2 Py, and then run:

python3 demo2.py

The operation results are as follows:

NAME
    demo2.py

SYNOPSIS
    demo2.py COMMAND

COMMANDS
    COMMAND is one of the following:

     double

As you can see, here it identifies the methods in the Calculator class. One of the commands is double. Let's try to call the following:

python3 demo2.py double

The operation results are as follows:

ERROR: The function received no value for the required argument: number
Usage: demo2.py double NUMBER

For detailed information on this command, run:
  demo2.py double --help

As we have said here, another parameter must be specified here, which is called NUMBER. At the same time, this parameter is still a required parameter. Let's try to add:

python3 demo2.py double 4

The operation results are as follows:

The correct result can be achieved at this time.

Therefore, in general, fire can be a class command line. Each command corresponds to a method name. At the same time, additional optional or required parameters are added after the command line parameters.

Rewrite

Finally, let's go back and add command line parameter support to the scratch method we defined at the beginning:

import requests
import fire

def scrape(url, timeout=10):
    response = requests.get(url, timeout=timeout)
    print(response.text)
    
    
if __name__ == '__main__':
    fire.Fire(scrape)

That's it! Is it very convenient to omit the lengthy argparse code?

The call is in the following form:

NAME
    main.py

SYNOPSIS
    main.py URL <flags>

POSITIONAL ARGUMENTS
    URL

FLAGS
    --timeout=TIMEOUT
        Default: 10

It is said here that URL is a required parameter and timeout is an optional parameter.

The last call is:

python3 main.py https://www.baidu.com 

In this way, we can easily pass the url through the command line.

Of course, timeout is also an optional value. We can specify the timeout parameter through - timeout ':

python3 main.py https://www.baidu.com --timeout 5

In this way, the two parameters can be assigned successfully. The final effect is to crawl Baidu and timeout for 5 seconds.

What about? Is it convenient? Let's use it!

Topics: Python Back-end