5

I'd like to add some command line switches to my script, to which I use the argparse.

The related part of my script so far looks like:

import argparse

parser = argparse.ArgumentParser(prog="Hola python",description="Hola")
parser.add_argument('-i', '--input', help="helpppping")
parser.print_help()

However this results in:

usage: Hola python [-h] [-i INPUT]

Hola

optional arguments:
-h, --help            show this help message and exit
-i INPUT, --input INPUT
                    helpppping

My concerns is with this row

-i INPUT, --input INPUT

This should looks like

 -i, --input         helppppping

I saw this question, and read this part of the manual, and all seems good but still not formatted well.

I obviously miss something just don't know what.

SecThor
  • 69
  • 7
  • So you basically want `-i` to work like a switch, and not take any argument after it? – Graipher Feb 21 '18 at 13:14
  • Well it should be followed with string to the input file, I just saw in many scripts that there are a short and a long version of the switch and both do the same. – SecThor Feb 21 '18 at 13:16
  • 1
    And they do. Have you tried using this? – Graipher Feb 21 '18 at 13:17
  • I believe you did correctly and it does what you want. You just misunderstand the help message. The helpppping text appears next line due to formatting, and the text says -i INPUT, --input INPUT rather than -i, --input INPUT, well, because that's the way it is. But functionaly, I think you're fine. – Jérôme Feb 21 '18 at 13:18
  • To my understanding add_argument first take a list of flags. So the '-short_version', '--long_version' should work, but its not. I'm a little confused. – SecThor Feb 21 '18 at 13:20
  • 3
    But what does not work? Please show a failure test case. – Jérôme Feb 21 '18 at 13:23
  • 1
    Based on the accepted answer your concern apparently is with the formatting the help line, not with actual parsing of inputs. If the `metavar` fix isn't enough (such as an extra space), look at past questions on the same topic, [python argparse help message, disable metavar for short options?](https://stackoverflow.com/questions/23936145) – hpaulj Feb 22 '18 at 00:54

1 Answers1

1

try this

 parser.add_argument('-i', '--input', metavar='', help='helping')
om tripathi
  • 300
  • 1
  • 5
  • 20
  • Gosh, that was quick. It works now, so I forgot to include the rest of the params? :O – SecThor Feb 21 '18 at 13:22
  • Thanks yes there are some formatter_class that you can use checkout this https://docs.python.org/3/library/argparse.html – om tripathi Feb 21 '18 at 13:26
  • So @SecThor you're just bothered by the fact the help reads 'INPUT'?! This is just cosmetic. I don't think there's a point addressing this and if I were you I'd leave it the way it is because this is what people are used to see. It doesn't matter either way, though. – Jérôme Feb 21 '18 at 13:30
  • I agree with you, this was just cosmetic, but my OCD forced me to fix it :) – SecThor Feb 21 '18 at 13:31
  • @Jérôme yes you are correct in my case my team tester want we to fix this and he has no idea about CLI tools it's very irritating sometimes – om tripathi Feb 21 '18 at 13:34
  • 1
    Why did you add the `required=True` and `type=str` option? Adding `required=True` changes the intent of the code that @SecThor presented, and `type=str` just adds noise because it is the default behavior. The only thing that needed to be added was `metavar=''`. – SethMMorton Feb 21 '18 at 18:02