How to easily write beautiful command line programs in Python

Python, originally used as a configuration management language at the time of its birth, has become one of the most popular programming languages, especially as the rise of AI applications has become ubiquitous.


The getopt and optparse modules in Python can be used to develop command-line programs, but they have been deprecated. The argparse module was introduced in versions 2.7 and 3.2; here we use the argparser module, which takes only a few dozen lines of code to make Beautiful command line program.

1. Basic command line

Take a look at an example from the official documentation:

The functions implemented by these lines of code are: If the command line input does not have the --sum option, the max operation is performed on multiple integers entered; otherwise, the sum operation is performed. For example: execute on the command line: python prog.py 1 2 3 4 output is: 4

Execute in the command line: python prog.py 1 2 3 4 --sum output is: 10

In the above code, the main process is divided into three steps:

Create an ArgumentParser object by calling the constructor of argparse.ArgumentParser.

Call the ArgumentParser's add_argument() method to add the command line parameters, including optional parameters and required parameters. The beginning of ---, - indicates optional parameters, and the other indicates the parameters that must be entered.

Call the parser.parse_args() method to parse the incoming sys.argv argument.

2. Implement subcommands

Sometimes, we can group the command line into a series of related subcommands according to the function. For example, the Git command line can be divided into the following groups:

In Python, such a subcommand can be easily made using the argparse module.

For example, we need to do a command-line program that manages user information, including adding, deleting, querying, and more.

We can divide it into three sub-commands according to the function: add, delete, show.

The corresponding code can be written like this:

Explain the above code outline: The add_subparsers() method of the ArgumentParser object creates a special action instance. Call the add_parser() method on the instance to create the ArgumentParser corresponding to the subcommand, and then call the add_argument() of the ArgumentParser instance. Method to add parameters to the subcommand. One convenient point is that the processing function corresponding to the subcommand can be set by the set_defaults() method of ArgumentParser. In the above example, the processing functions are set for the three subcommands: add_fn, delete_fn, show_fn.

Executing in the command line program: python cmd.py --help will display the following information:

As you can see from the help information, the command line is organized into three subcommands, add, delete, and show.

Execute in the command line: python cmd.py add --help , you can view the help information of the add subcommand:

For the add subcommand, it has three parameters, namely the user name, contact address, and phone number; the user name is a mandatory parameter, and the other two are optional parameters.

Execute in the command line: python cmd.py add --name 'Eric' --addr 'California,USA' --phone '0076938377'`

Adding a User's processing logic will be done by add_fn(). The program will get a User with the message {name='Eric', addr='California, USA', phone='0076938377'}.

Other delete and show subcommands are similar to this and will not be described again.

3. Summary

Using Python's argparse module, a few dozen lines of code can be used to build a skeleton of a command-line program.

The most important part of the argparse module is the ArgumentParser object, which contains the following methods:

Add_argument()

Add_subparsers()

Parse_args()


Micro Vibration Motor

The micro Vibration Motor is introduced:

Micro vibration motor is mainly used for adult health care products and toys, micro vibration motor is a Dc Motor, the motor shaft with an eccentric wheel, the use of most of the micro vibration motor voltage is lower than 6 v, probably between 6 mm to 15 mm in diameter, when the motor rotation, eccentric circle particles is not the core of the motor, the motor often lose their balance, due to the inertia effect.



Characteristics: small volume, strong vibration;


Features: small size, fast speed, stable performance, low price, can use battery drive,Can change the different materials of the pendulum head
Vibration Motor Vibration Motor
Method of use: the best stable in horizontal plane, installed on the dc Micro vibration motor output shaft parts, cannot use a hammer to knock, knock prone to press into the Micro vibration motor drive, may cause damage to internal components, and cannot be used in the case of blocked.

vibration motor

Operating temperature range:

Mini Vibration Motor should be used at a temperature of -10~60℃.

The figures stated in the catalog specifications are based on use at ordinary room temperature catalog specifications re based on use at ordinary room temperature (approximately20~25℃.

If a Mini Vibration Motor is used outside the prescribed temperature range,the grease on the gearhead area will become unable to function normally and the motor will become unable to start.Depending on the temperature conditions ,it may be possible to deal with them by changing the grease of the motor's parts.Please feel free to consult with us about this.


Storage temperature range:

Mini Vibration Motor should be stored ta a temperature of -15~65℃.

In case of storage outside this range,the grease on the gearhead area will become unable to function normally and the motor will become unable to start.


Service life:

The longevity of Mini Vibration Motor is greatly affected by the load conditions , the mode of operation,the environment of use ,etc.Therefore,it is necessary to check the conditions under which the product will actually be used .The following conditions will have a negative effect on longevity.Please consult with us should any of them apply.


●Use with a load that exceeds the rated torque

●Frequent starting

●Momentary reversals of turning direction

●Impact loads

●Long-term continuous operation

●Forced turning using the output shaft

●Use in which the permitted overhang load or the permitted thrust load is exceeded

●A pulse drive ,e.g.,a short break,counter electromotive force,PWM control

●Use of a voltage that is nonstandard as regards the rated voltage

●Use outside the prescribed temperature or relative-humidity range,or in a special environment.

●Please consult with us about these or any other conditions of use that may apply,so that we can be sure that you select the most appropriate model.


when it come to volume production,we're a major player as well .each month,we rurn out 600000 units,all of which are compliant with the rohs directive.Have any questions or special needed, please contact us, we have the engineer group and best sales department to service to you Looking forward to your inquiry. Welcome to our factory.

vibration motor

Micro Vibration Motor,Mini Vibration Motor,Dc Micro Vibration Motor,Electric Micro Vibration Motor

Shenzhen Shunchang Motor Co., LTD. , https://www.scgearmotor.com