简体   繁体   中英

How to add arguments in a command in Linux

I am making a Linux application which synchronizes the User's Desktop data with the cloud. I made a python executable file clouddrive which can launch the application. I copied this file into /usr/local/bin . Now, If I type clouddrive on the terminal, then it opens the application. But I want to add some arguments to that command. Suppose If user type clouddrive --logout , then the user should be logged out. If user type clouddrive --preferences , then preferences panel should be opened. I want to know that how can I add these arguments to that command?

Use argparse module.

Example:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo", help="echo the string you use here")
args = parser.parse_args()
print args.echo

Result:

$ python prog.py -h
usage: prog.py [-h] echo

positional arguments:
  echo        echo the string you use here

optional arguments:
  -h, --help  show this help message and exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM