-1

Example screenshot: My desired output: Image I wanted to enable help flags for users of my scripts, and initially thought the solution is to use argparse:


parser = argparse.ArgumentParser(description='... blah blah ...')
parser.add_argument('file1', help='... blah blah ...')
parser.add_argument('file2', help='... blah blah ...')

For some reason, I can't print to the cmd prompt, since adding -h (like red circle in my example) returns a FileNotFound Error instead. Is the above code not correct?

If so, could you provide me the code? Or are there any helpful sources (I've been searching for a definitive example, to no avail yet)

Community
  • 1
  • 1
  • 3
    Welcome to StackOverflow. See [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). We cannot effectively help you until you post your MRE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Also, please make sure that you've properly researched examples before posting. Asking for sources is specifically off-topic for Stack Overflow. – Prune Jan 02 '20 at 18:55
  • 1
    Does this answer your question? [Display help message with python argparse when script is called without any arguments](https://stackoverflow.com/questions/4042452/display-help-message-with-python-argparse-when-script-is-called-without-any-argu) – gold_cy Jan 02 '20 at 18:56
  • 1
    Here's some [supplemental information](https://pymotw.com/3/argparse/index.html) about using the `argparse` module which may help. See the section on [Customizing Help](https://pymotw.com/3/argparse/index.html#customizing-help). – martineau Jan 02 '20 at 19:04
  • @Prune Yes, I would have posted better MRE code than 3 lines above, however, doing so here would have resulted in a massive code block - especially since it turned out (from source provided by @aws_apprentice) I'd missed out one line of code: `args = parser.parse_args()`. But I understand your concern. –  Jan 02 '20 at 19:18
  • @aws_apprentice Thank-you, it now works after I added args = parser.parse_args() after the 3 lines of code. But do you know why this is so? It seems I could substitute _any_ name in place for 'args' variable; does the interpreter somehow use this behind-the-scenes? –  Jan 02 '20 at 19:21

2 Answers2

0

Try adding

parser = argparse.ArgumentParser(add_help=True)

Kenan
  • 13,156
  • 8
  • 43
  • 50
0

I'm unable to reproduce this.

so59568543.py

import argparse

parser = argparse.ArgumentParser(description='... blah blah ...')
parser.add_argument('file1', help='... blah blah ...')
parser.add_argument('file2', help='... blah blah ...')
args = parser.parse_args()

print(args)

Windows 10, Python 3.7.3

C:\Users\X\Desktop>python so59568543.py
usage: so59568543.py [-h] file1 file2
so59568543.py: error: the following arguments are required: file1, file2

C:\Users\X\Desktop>python so59568543.py -h
usage: so59568543.py [-h] file1 file2

... blah blah ...

positional arguments:
  file1       ... blah blah ...
  file2       ... blah blah ...

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

C:\Users\X\Desktop>python so59568543.py aaa bbb
Namespace(file1='aaa', file2='bbb')

C:\Users\X\Desktop>

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Yes, it was too long to reproduce, especially since the error was simply omitting 'args = parser.parse_args()`, as you included here - thanks. Do you know what this extra line of code does? Does the interpreter perhaps use this variable unbeknown to me? –  Jan 02 '20 at 19:40
  • 1
    `parse_args()` does the actual parsing of command line options and arguments. Without it, the parser actually does nothing at all. – AKX Jan 02 '20 at 20:02