Make my python command line program interactive with argparse

I am trying to make my python program interactive on the command line, the user should be able to do things like:

python myprogram.py --create 

then

 python myprogram.py --send 

The problem is when the program stops and restarts every time, so I lose my variable and the object that I created with the first command.

I use argparse as follows:

 parser = argparse.ArgumentParser() parser.add_argument('-c','--create' ,help='',action='store_true') parser.add_argument('-s','--send',help='',action='store_true') args = parser.parse_args() if args.create: create() elif args.send : send() 

I do not want to stop the program between the team, how to do it?

example: https://coderwall.com/p/w78iva

+5
source share
2 answers

Here is a simple interactive script. I use argparse to parse input strings, but otherwise it is not essential to the action. However, this can be a convenient way to add parameters to the create command. For example, ipython uses argparse to process its %magic commands:

 import argparse parser = argparse.ArgumentParser(prog='PROG', description='description') parser.add_argument('cmd', choices=['create','delete','help','quit']) while True: astr = raw_input('$: ') # print astr try: args = parser.parse_args(astr.split()) except SystemExit: # trap argparse error message print 'error' continue if args.cmd in ['create', 'delete']: print 'doing', args.cmd elif args.cmd == 'help': parser.print_help() else: print 'done' break 

This can be divided into a while , a raw_input string raw_input and astr own variable estimate.

The key to using argparse here are:

  • parse_args can take a list of strings ( split() result) instead of using sys.argv[1:] by default.
  • If parse_args sees the problem (or '-h'), it prints a message and tries to "exit". If you want to continue, you need to catch this error, so try .
  • parse_args output is a simple namespace object. You access the arguments as attributes.
  • You can easily replace your own parser.
+5
source

The differential in cmd and argparse is that cmd is a "line-oriented command interpreter" and argparse is a parser for sys.argv .

In your example, sys.argv analyzed, which you pass during the start of your program, and then, if it receives a value, you start the function and then exit.

argparse will only sys.argv during program startup.

You can add code for working with args , which you pass as a function or class, or make a program menu that you can use with raw_input . Example:

 class Main(): def __init__(self, create=None, send=None): if create: self.create(create) elif send: self.send(send) option = raw_input('What do you want to do now?') print option def create(self, val): print val def send(self, val): print val if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-c','--create' ,help='',action='store_true') parser.add_argument('-s','--send',help='',action='store_true') args = parser.parse_args() Main(args.create, args.send) 

Other then python argparse and control / override of exit status code or python argparse - add action to subparse without arguments? can help.

The first shows how you can override quit, and the second shows you how to add subcommands or quitactions.

0
source

Source: https://habr.com/ru/post/1204497/


All Articles