initiating python script with arguments from another script, setting argparse values programatically - python

i was using a python package (spotify_dl) to genereate some files. i was doing it manually through the command line:
spotify_dl -l spotify_playlist_link -o download_directory
now i want to do it from inside another python script.
i looked at the package code and found the main function, but i can't figure how to run the main function with arguments of my choosing - in python.
for example what i want:
from spotify_dl import spotify_dl as sp_dl
if __name__=='__main__':
destination_dir = r'D:\some\folder\path'
playlists_url = ['url1','url2','url3',....]
for url in playlists_url:
sp_dl.spotify_dl(l=url,o=destination_dir)
for better clarity, here is the actual code of the spotify_dl main function:
#!/usr/bin/env python
import argparse
import json
import os
import sys
from logging import DEBUG
from pathlib import Path, PurePath
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from spotify_dl.constants import VERSION
from spotify_dl.models import db, Song
from spotify_dl.scaffold import log, check_for_tokens
from spotify_dl.spotify import fetch_tracks, parse_spotify_url, validate_spotify_url, get_item_name
from spotify_dl.youtube import download_songs, default_filename, playlist_num_filename
def spotify_dl():
"""Main entry point of the script."""
parser = argparse.ArgumentParser(prog='spotify_dl')
parser.add_argument('-l', '--url', action="store",
help="Spotify Playlist link URL", type=str, required=True)
parser.add_argument('-o', '--output', type=str, action='store',
help='Specify download directory.', required=True)
parser.add_argument('-d', '--download', action='store_true',
help='Download using youtube-dl', default=True)
parser.add_argument('-f', '--format_str', type=str, action='store',
help='Specify youtube-dl format string.',
default='bestaudio/best')
parser.add_argument('-k', '--keep_playlist_order', default=False,
action='store_true',
help='Whether to keep original playlist ordering or not.')
parser.add_argument('-m', '--skip_mp3', action='store_true',
help='Don\'t convert downloaded songs to mp3')
parser.add_argument('-s', '--scrape', action="store",
help="Use HTML Scraper for YouTube Search", default=True)
parser.add_argument('-V', '--verbose', action='store_true',
help='Show more information on what''s happening.')
parser.add_argument('-v', '--version', action='store_true',
help='Shows current version of the program')
args = parser.parse_args()
if args.version:
print("spotify_dl v{}".format(VERSION))
exit(0)
db.connect()
db.create_tables([Song])
if os.path.isfile(os.path.expanduser('~/.spotify_dl_settings')):
with open(os.path.expanduser('~/.spotify_dl_settings')) as file:
config = json.loads(file.read())
for key, value in config.items():
if value and (value.lower() == 'true' or value.lower() == 't'):
setattr(args, key, True)
else:
setattr(args, key, value)
if args.verbose:
log.setLevel(DEBUG)
log.info('Starting spotify_dl')
log.debug('Setting debug mode on spotify_dl')
if not check_for_tokens():
exit(1)
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
log.debug('Arguments: {}'.format(args))
if args.url:
valid_item = validate_spotify_url(args.url)
if not valid_item:
sys.exit(1)
if args.output:
item_type, item_id = parse_spotify_url(args.url)
directory_name = get_item_name(sp, item_type, item_id)
save_path = Path(PurePath.joinpath(Path(args.output), Path(directory_name)))
save_path.mkdir(parents=True, exist_ok=True)
log.info("Saving songs to: {}".format(directory_name))
songs = fetch_tracks(sp, item_type, args.url)
if args.download is True:
file_name_f = default_filename
if args.keep_playlist_order:
file_name_f = playlist_num_filename
download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order, file_name_f)
if __name__ == '__main__':
spotify_dl()
so far i could see mentions of sys.argv but also some comments spoke against using that.
what i want is a clear method that i could run in a loop without complications.

spotify_dl is badly designed, I commonly use this:
def parse_args(argv=None):
parser = argparse.ArgumentParser()
...
return parser.parse_args(argv)
def main(args):
# run your code
if __name__ == "__main__":
args = parse_args()
main(args)
Then you can import this first script in another and call the main function:
from my_first_script import main
def a_function():
args = namedtuple("args", ("arg1", ...))("value1")
main(args)
But you can workaround by overriding sys.argv:
if __name__=='__main__':
destination_dir = r'D:\some\folder\path'
playlists_url = ['url1','url2','url3',....]
for url in playlists_url:
sys.argv = [sys.executable, "-o", destination_dir, "-l", url]
sp_dl.spotify_dl()

You can use subprocess here.
I did not try your example because I don't have those libraries installed, but here's a simple example.
First, a simple commandline script called testpy.py has the following:
import sys
import argparse
def check_arg(args=None):
parser = argparse.ArgumentParser(prog='Test',
description='Test',
epilog='Test')
parser.add_argument('-f', '--first',
help='First argument',
required='True')
results = parser.parse_args(args)
return (results.first)
def main():
with open('test.txt', 'a') as file:
file.write('Success, {}\n'.format(f))
if __name__ == '__main__':
f = check_arg(sys.argv[1:])
main()
This takes one argument f and exports a text file (test.txt) with whatever argument was supplied.
This script can be run from a next script as (save this as new script file):
from subprocess import run, PIPE
args = ['python3', 'testpy.py', '-f', 'First input argument text']
res = run(args, stdout=PIPE, stderr=PIPE)
If you run this, you'll see that the file test.txt will be exported and calling the script testpy.py from this script was successful.

Related

Command line argument to run my own script

I have written a code function.py in python which has input file path and a output file path and some flags . currently I have hardcoded everything.I want to use command line arguments to provide these inputs so that anyone can run my script by providing input to cmd.how can I do it in python?
In CMD
function.py "input file path" "output file path"
A very rudimentary example would be:
import sys
input_file_path = sys.argv[1]
output_file_path = sys.argv[2]
Note that sys.argv[0] would be your filename. You should also do the relevant checks to make sure there are the correct number of arguments, whether they are valid, etc.
As an alternative to sys.argv, I prefer argparse.
As an example:
# Import the argparse module
import argparse
# Define a function to use argparse to parse your command-line arguments
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--input-file",
dest="input_file",
help="File to use as input",
type=str
)
parser.add_argument(
"-o",
"--output-file",
dest="output_file",
help="File to output to",
type=str
)
return parser.parse_args()
# If calling this module from the command line, this `if` statement will evaluate to True
if __name__ == "__main__":
# Parse your command-line arguments
args = parse_args()
# Get the parsed value of the "-i" argument:
infile = args.input_file
# Get the parsed value of the "-o" argument:
outfile = args.output_file

Confusion in using argparse in Python

I would like to execute a code which is accessible from this link.
The code objective is to read and extract the pdf annotation.
However, I am not sure how to direct the pdf file path using the argparse, which I suspect, should be the following argparse.
p.add_argument("input", metavar="INFILE", type=argparse.FileType("rb"),
help="PDF files to process", nargs='+')
Say, I know the absolute path of the pdf file is as follow;
C:\abc.pdf
Also, given that I still try to comprehend the code, so I would like to avoid re-enter the path C:\abc.pdf over and over again. Is there are way, I can temporary hard coded it within the def parse_args()
I have read several thread about this topic, however, still having difficulties in comprehend about this issue.
Thanks in advance for any insight.
You add the argument to a parser:
import argparse
p = argparse.ArgumentParser()
p.add_argument("input", metavar="INFILE", type=argparse.FileType("rb"),
help="PDF files to process", nargs='+')
args = p.parse_args()
Then args.input will be a tuple of one or more open file handles.
If you're running pdfannots.py from the command prompt, then you do so as, e.g.,
python pdfannots.py C:\abc.pdf
If you want to run it over multiple PDF files, then you do so as, e.g.,
python pdfannots.py C:\abc.pdf D:\xyz.pdf E:\foo.pdf
If you really want to hard code the path, you'll have to edit pdfannots.py as follows:
def parse_args():
p = argparse.ArgumentParser(description=__doc__)
# p.add_argument("input", metavar="INFILE", type=argparse.FileType("rb"),
# help="PDF files to process", nargs='+')
g = p.add_argument_group('Basic options')
g.add_argument("-p", "--progress", default=False, action="store_true",
help="emit progress information")
g.add_argument("-o", metavar="OUTFILE", type=argparse.FileType("w"), dest="output",
default=sys.stdout, help="output file (default is stdout)")
g.add_argument("-n", "--cols", default=2, type=int, metavar="COLS", dest="cols",
help="number of columns per page in the document (default: 2)")
g = p.add_argument_group('Options controlling output format')
allsects = ["highlights", "comments", "nits"]
g.add_argument("-s", "--sections", metavar="SEC", nargs="*",
choices=allsects, default=allsects,
help=("sections to emit (default: %s)" % ', '.join(allsects)))
g.add_argument("--no-group", dest="group", default=True, action="store_false",
help="emit annotations in order, don't group into sections")
g.add_argument("--print-filename", dest="printfilename", default=False, action="store_true",
help="print the filename when it has annotations")
g.add_argument("-w", "--wrap", metavar="COLS", type=int,
help="wrap text at this many output columns")
return p.parse_args()
def main():
args = parse_args()
global COLUMNS_PER_PAGE
COLUMNS_PER_PAGE = args.cols
for file in [open(r"C:\Users\jezequiel\Desktop\Timeline.pdf", "rb")]:
(annots, outlines) = process_file(file, args.progress)
pp = PrettyPrinter(outlines, args.wrap)
if args.printfilename and annots:
print("# File: '%s'\n" % file.name)
if args.group:
pp.printall_grouped(args.sections, annots, args.output)
else:
pp.printall(annots, args.output)
return 0

Convert __main__ function from Python script intended for command line into a callable function

I am working with someone's GitHub code that is designed to be called from the command line like so:
> python this_script.py -u <username> -p <password> -i <id_num> ...
This produces an output text file after parsing the inputs in such a manner within this_script.py:
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='get data', add_help=False, usage='this_script.py -u username -p password [options]')
parser.add_argument('-u', metavar='<STR>', type=str, help='username')
parser.add_argument('-p', metavar='<STR>', type=str, help='password')
parser.add_argument('-i', metavar='<STR>', nargs='+', type=str, help='List of IDs')
...
I want to use many of these text files within my own set of code, so it would be much more convenient for me to convert this from a command line script to a callable function, with syntax something like this:
def this_script(password, username, *args):
...
Is there a simple way to do this, without have to mess around too much with the inner workings of the __main__ block as currently defined?
How about:
def do_stuff(args):
print(args.u)
print(args.p)
def main(args_list=None):
parser = argparse.ArgumentParser(description='get data', add_help=False, usage='this_script.py -u username -p password [options]')
parser.add_argument('-u', metavar='<STR>', type=str, help='username')
parser.add_argument('-p', metavar='<STR>', type=str, help='password')
parser.add_argument('-i', metavar='<STR>', nargs='+', type=str, help='List of IDs')
...
if args_list:
args= parser.parse_args(args_list)
else:
args = parser.parse_args()
do_stuff(args)
if __name__ == "__main__":
main()
By adding the following line into your package __init__.py
from .__main__ import main
You can call the script as a function from command line, or from another script as long as you import your package, such as
import <package name> as pkg
pkg.main(['-u', 'my_username', '-p', 'my_password'])
Yes, the simple way is to use stdlib runpy.
import runpy
runpy.run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
And, please make a mental note never to write your own scripts like that. There should only ever be a single line under the conditional, and it should be a call to a function with no arguments, like this:
if __name__ == "__main__":
main()
Anything more is too much.

path to a directory as argparse argument

I want to accept a directory path as user input in an add_argument() of ArgumentParser().
So far, I have written this:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('path', option = os.chdir(input("paste here path to biog.txt file:")), help= 'paste path to biog.txt file')
What would be the ideal solution to this problem?
One can ensure the path is a valid directory with something like:
import argparse, os
def dir_path(string):
if os.path.isdir(string):
return string
else:
raise NotADirectoryError(string)
parser = argparse.ArgumentParser()
parser.add_argument('--path', type=dir_path)
# ...
Check is possible for files using os.path.isfile() instead, or any of the two using os.path.exists().
Argument Parser(argparse) Examples : Different type of arguments with custom handlers added. For PATH you can pass "-path" followed by path value as argument
import os
import argparse
from datetime import datetime
def parse_arguments():
parser = argparse.ArgumentParser(description='Process command line arguments.')
parser.add_argument('-path', type=dir_path)
parser.add_argument('-e', '--yearly', nargs = '*', help='yearly date', type=date_year)
parser.add_argument('-a', '--monthly', nargs = '*',help='monthly date', type=date_month)
return parser.parse_args()
def dir_path(path):
if os.path.isdir(path):
return path
else:
raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")
def date_year(date):
if not date:
return
try:
return datetime.strptime(date, '%Y')
except ValueError:
raise argparse.ArgumentTypeError(f"Given Date({date}) not valid")
def date_month(date):
if not date:
return
try:
return datetime.strptime(date, '%Y/%m')
except ValueError:
raise argparse.ArgumentTypeError(f"Given Date({date}) not valid")
def main():
parsed_args = parse_arguments()
if __name__ == "__main__":
main()
You can use something like:
import argparse, os
parser = argparse.ArgumentParser()
parser.add_argument('--path', help= 'paste path to biog.txt file')
args = parser.parse_args()
os.chdir(args.path) # to change directory to argument passed for '--path'
print os.getcwd()
Pass the directory path as an argument to --path while running your script. Also, check the official document for correct usage of argparse: https://docs.python.org/2/howto/argparse.html

Display help message with Python argparse when script is called without any arguments

Assume I have a program that uses argparse to process command line arguments/options. The following will print the 'help' message:
./myprogram -h
or:
./myprogram --help
But, if I run the script without any arguments whatsoever, it doesn't do anything. What I want it to do is to display the usage message when it is called with no arguments. How is that done?
This answer comes from Steven Bethard on Google groups. I'm reposting it here to make it easier for people without a Google account to access.
You can override the default behavior of the error method:
import argparse
import sys
class MyParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
self.print_help()
sys.exit(2)
parser = MyParser()
parser.add_argument('foo', nargs='+')
args = parser.parse_args()
Note that the above solution will print the help message whenever the error
method is triggered. For example, test.py --blah will print the help message
too if --blah isn't a valid option.
If you want to print the help message only if no arguments are supplied on the
command line, then perhaps this is still the easiest way:
import argparse
import sys
parser=argparse.ArgumentParser()
parser.add_argument('foo', nargs='+')
if len(sys.argv)==1:
parser.print_help(sys.stderr)
sys.exit(1)
args=parser.parse_args()
Note that parser.print_help() prints to stdout by default. As init_js suggests, use parser.print_help(sys.stderr) to print to stderr.
Instead of writing a class, a try/except can be used instead
try:
options = parser.parse_args()
except:
parser.print_help()
sys.exit(0)
The upside is that the workflow is clearer and you don't need a stub class. The downside is that the first 'usage' line is printed twice.
This will need at least one mandatory argument. With no mandatory arguments, providing zero args on the commandline is valid.
With argparse you could use ArgumentParser.print_usage():
parser.argparse.ArgumentParser()
# parser.add_args here
# sys.argv includes a list of elements starting with the program
if len(sys.argv) < 2:
parser.print_usage()
sys.exit(1)
Printing help
ArgumentParser.print_usage(file=None)
  Print a brief description of how the ArgumentParser should be invoked on the command line. If file is None, sys.stdout is assumed.
The cleanest solution will be to manually pass default argument if none were given on the command line:
parser.parse_args(args=None if sys.argv[1:] else ['--help'])
Complete example:
import argparse, sys
parser = argparse.ArgumentParser()
parser.add_argument('--host', default='localhost', help='Host to connect to')
# parse arguments
args = parser.parse_args(args=None if sys.argv[1:] else ['--help'])
# use your args
print("connecting to {}".format(args.host))
This will print complete help (not short usage) if called w/o arguments.
If you associate default functions for (sub)parsers, as is mentioned under add_subparsers, you can simply add it as the default action:
parser = argparse.ArgumentParser()
parser.set_defaults(func=lambda x: parser.print_usage())
args = parser.parse_args()
args.func(args)
Add the try-except if you raise exceptions due to missing positional arguments.
If you have arguments that must be specified for the script to run - use the required parameter for ArgumentParser as shown below:-
parser.add_argument('--foo', required=True)
parse_args() will report an error if the script is run without any arguments.
Throwing my version into the pile here:
import argparse
parser = argparse.ArgumentParser()
args = parser.parse_args()
if not vars(args):
parser.print_help()
parser.exit(1)
You may notice the parser.exit - I mainly do it like that because it saves an import line if that was the only reason for sys in the file...
There are a pair of one-liners with sys.argv[1:] (a very common Python's idiom to refer the command line arguments, being sys.argv[0] the script's name) that can do the job.
The first one is self-explanatory, clean and pythonic:
args = parser.parse_args(None if sys.argv[1:] else ['-h'])
The second one is a little hackier. Combining the previously evaluated fact that an empty list is False with the True == 1 and False == 0 equivalences you get this:
args = parser.parse_args([None, ['-h']][not sys.argv[1:]])
Maybe too many brackets, but pretty clear if a previous argument selection was made.
_, *av = sys.argv
args = parser.parse_args([None, ['-h']][not av])
parser.print_help()
parser.exit()
The parser.exit method also accept a status (returncode), and a message value (include a trailing newline yourself!).
an opinionated example,
:)
#!/usr/bin/env python3
""" Example argparser based python file
"""
import argparse
ARGP = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawTextHelpFormatter,
)
ARGP.add_argument('--example', action='store_true', help='Example Argument')
def main(argp=None):
if argp is None:
argp = ARGP.parse_args() # pragma: no cover
if 'soemthing_went_wrong' and not argp.example:
ARGP.print_help()
ARGP.exit(status=64, message="\nSomething went wrong, --example condition was not set\n")
if __name__ == '__main__':
main() # pragma: no cover
Example calls:
$ python3 ~/helloworld.py; echo $?
usage: helloworld.py [-h] [--example]
Example argparser based python file
optional arguments:
-h, --help show this help message and exit
--example Example Argument
Something went wrong, --example condition was not set
64
$ python3 ~/helloworld.py --example; echo $?
0
Most of the answers here required another module, such as sys, to be imported or were using optional arguments. I wanted to discover an answer that used only argparse, worked with required arguments, and if possible worked without catching exceptions. I ended up with the following:
import argparse
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(add_help=False)
arg_parser.add_argument('input_file', type=str, help='The path to the input file.')
arg_parser.add_argument('output_file', type=str, help='The path to the output file.')
arg_parser.add_argument('-h','--help', action='store_true', help='show this help message and exit')
arg_parser.usage = arg_parser.format_help()
args = arg_parser.parse_args()
The main idea was to use the format_help function in order to provide the help string to the usage statement. Setting add_help to False in the call to ArgumentParser() prevents the help statement from printing twice in certain circumstances. However, I had to create an argument for the optional help argument that mimicked the typical help message once it was set to False in order to display the optional help argument in the help message. The action is set to store_true in the help argument to prevent the help message from filling in a value like HELP for the parameter when it prints the help message.
So for a really simple answer. Most of the time with argparse you are checking to see if parameters are set anyway, to call a function that does something.
If no parameters, just else out at the end and print the help. Simple and works.
import argparse
import sys
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("--holidays", action='store_true')
group.add_argument("--people", action='store_true')
args=parser.parse_args()
if args.holidays:
get_holidays()
elif args.people:
get_people()
else:
parser.print_help(sys.stderr)
Here is another way to do it, if you need something flexible where you want to display help if specific params are passed, none at all or more than 1 conflicting arg:
import argparse
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--days', required=False, help="Check mapped inventory that is x days old", default=None)
parser.add_argument('-e', '--event', required=False, action="store", dest="event_id",
help="Check mapped inventory for a specific event", default=None)
parser.add_argument('-b', '--broker', required=False, action="store", dest="broker_id",
help="Check mapped inventory for a broker", default=None)
parser.add_argument('-k', '--keyword', required=False, action="store", dest="event_keyword",
help="Check mapped inventory for a specific event keyword", default=None)
parser.add_argument('-p', '--product', required=False, action="store", dest="product_id",
help="Check mapped inventory for a specific product", default=None)
parser.add_argument('-m', '--metadata', required=False, action="store", dest="metadata",
help="Check mapped inventory for specific metadata, good for debugging past tix", default=None)
parser.add_argument('-u', '--update', required=False, action="store_true", dest="make_updates",
help="Update the event for a product if there is a difference, default No", default=False)
args = parser.parse_args()
days = args.days
event_id = args.event_id
broker_id = args.broker_id
event_keyword = args.event_keyword
product_id = args.product_id
metadata = args.metadata
make_updates = args.make_updates
no_change_counter = 0
change_counter = 0
req_arg = bool(days) + bool(event_id) + bool(broker_id) + bool(product_id) + bool(event_keyword) + bool(metadata)
if not req_arg:
print("Need to specify days, broker id, event id, event keyword or past tickets full metadata")
parser.print_help()
sys.exit()
elif req_arg != 1:
print("More than one option specified. Need to specify only one required option")
parser.print_help()
sys.exit()
# Processing logic here ...
Cheers!
I like to keep things as simple as possible, this works great:
#!/usr/bin/env python3
Description = """Tool description"""
Epilog = """toolname.py -a aflag -b bflag with these combined it does blah"""
arg_parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=Description,
epilog=Epilog,
)
try:
if len(sys.argv) == 1:
arg_parser.print_help()
except Exception as e:
print(e)
This is how I start all my tools as its always good to include some examples
When call add_subparsers method save the first positional argument to dest= and check value after argparse has been initialized, like this:
subparsers = parser.add_subparsers(dest='command')
And just check this this variable:
if not args.command:
parser.print_help()
parser.exit(1) # If exit() - exit code will be zero (no error)
Full example:
#!/usr/bin/env python
""" doc """
import argparse
import sys
parser = argparse.ArgumentParser(description=__doc__)
subparsers = parser.add_subparsers(dest='command',
help='List of commands')
list_parser = subparsers.add_parser('list',
help='List contents')
list_parser.add_argument('dir', action='store',
help='Directory to list')
create_parser = subparsers.add_parser('create',
help='Create a directory')
create_parser.add_argument('dirname', action='store',
help='New directory to create')
create_parser.add_argument('--read-only', default=False, action='store_true',
help='Set permissions to prevent writing to the directory')
args = parser.parse_args()
if not args.command:
parser.print_help()
parser.exit(1)
print(vars(args)) # For debug
This approach is a lot more elegant than most others. Instead of overriding error(), you can control the behaviour a lot more precisely by wrapping the parse_args() method:
import sys
import argparse
HelpFlags = ('help', '--help', '-h', '/h', '?', '/?', )
class ArgParser (argparse.ArgumentParser):
def __init__(self, *args, **kws):
super().__init__(*args, **kws)
def parse_args(self, args=None, namespace=None):
if args is None:
args = sys.argv[1:]
if len(args) < 1 or (args[0].lower() in HelpFlags):
self.print_help(sys.stderr)
sys.exit()
return super().parse_args(args, namespace)
Set your positional arguments with nargs, and check if positional args are empty.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', nargs='?')
args = parser.parse_args()
if not args.file:
parser.print_help()
Reference Python nargs
If your command is something where a user needs to choose some action, then use a mutually exclusive group with required=True.
This is kind of an extension to the answer given by pd321.
import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--batch", action='store', type=int, metavar='pay_id')
group.add_argument("--list", action='store_true')
group.add_argument("--all", action='store_true', help='check all payments')
args=parser.parse_args()
if args.batch:
print('batch {}'.format(args.batch))
if args.list:
print('list')
if args.all:
print('all')
Output:
$ python3 a_test.py
usage: a_test.py [-h] (--batch pay_id | --list | --all)
a_test.py: error: one of the arguments --batch --list --all is required
This only give the basic help. And some of the other answers will give you the full help. But at least your users know they can do -h
This isn't good (also, because intercepts all errors), but:
def _error(parser):
def wrapper(interceptor):
parser.print_help()
sys.exit(-1)
return wrapper
def _args_get(args=sys.argv[1:]):
parser = argparser.ArgumentParser()
parser.error = _error(parser)
parser.add_argument(...)
...
Here is definition of the error function of the ArgumentParser class.
As you see, the following signature takes two arguments. However, functions outside the class know nothing about first argument self, because, roughly speaking, this argument is for the class.
def _error(self, message):
self.print_help()
sys.exit(-1)
def _args_get(args=sys.argv[1:]):
parser = argparser.ArgumentParser()
parser.error = _error
...
will output:
"AttributeError: 'str' object has no attribute 'print_help'"
You can pass parser (self) in _error function, by calling it:
def _error(self, message):
self.print_help()
sys.exit(-1)
def _args_get(args=sys.argv[1:]):
parser = argparser.ArgumentParser()
parser.error = _error(parser)
...
But if you don't want exit the program right now, return it:
def _error(parser):
def wrapper():
parser.print_help()
sys.exit(-1)
return wrapper
Nonetheless, parser doesn't know that it has been modified. Thus, when an error occurs, it will print the cause of it (by the way, it's a localized translation). So intercept it:
def _error(parser):
def wrapper(interceptor):
parser.print_help()
sys.exit(-1)
return wrapper
Now, when an error occurs, parser will print the cause of it, and you'll intercept it, look at it, and... throw out.

Categories