OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

argparse: Lots of choices results in ugly help output

  • Thread starter Thread starter Deleted
  • Start date Start date
D

Deleted

Guest
I have this code which I am generally pleased with:

Code:
import argparse

servers = [
    "ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer",
    "SkeppServer", "HavsServer", "PiratServer", "SvartServer", "NattServer",
    "SovServer"]

parser = argparse.ArgumentParser(
    description="A program to update components on servers.")
group = parser.add_mutually_exclusive_group()
group.add_argument(
    '-l',
    '--list',
    dest="update",
    action='store_false',
    default=False,
    help='list server components')
group.add_argument(
    '-u',
    '--updatepom',
    dest="update",
    action='store_true',
    help='update server components')
parser.add_argument(
    '-o',
    '--only',
    nargs='*',
    choices=servers,
    help='Space separated list of case sensitive server names to process')
parser.add_argument(
    '-s',
    '--skip',
    nargs='*',
    choices=servers,
    help='Space separated list of case sensitive server names to exclude from processing')
args = parser.parse_args()

I like that the choice=servers validates the server names in the input for me, so that I won´t have to. However, having so many valid choices makes the help output look terrible:

Code:
usage: args.py [-h] [-l | -u]
               [-o [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]]
               [-s [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]]

A program to update components on servers.

optional arguments:
  -h, --help            show this help message and exit
  -l, --list            list server components
  -u, --updatepom       update server components
  -o [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]], --only [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]
                        Space separated list of case sensitive server names to
                        process
  -s [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]], --skip [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]
                        Space separated list of case sensitive server names to
                        exclude from processing

Which way would you recommend if I want:

  • Nice (mostly) auto-generated help output
  • Validation that the entries given to the -o or the -s options are in servers.

Bonus:

  • Would it be possible to have case insensitive string matching for the server names?

Append

I tried using michaelfilms suggestion where the -o -s options are removed from the above output and this part is added:

Code:
server optional arguments:
  Valid server names are: ApaServer, BananServer, GulServer, SolServer,
  RymdServer, SkeppServer, HavsServer, PiratServer, SvartServer,
  NattServer, SovServer

I think it looks pretty good, but I really need to provide help for -o and -s options as the user wouldn´t know about them otherwise. So I am not all the way there yet using this approach.
<p>I have this code which I am generally pleased with:</p>
<pre><code>import argparse

servers = [
"ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer",
"SkeppServer", "HavsServer", "PiratServer", "SvartServer", "NattServer",
"SovServer"]

parser = argparse.ArgumentParser(
description="A program to update components on servers.")
group = parser.add_mutually_exclusive_group()
group.add_argument(
'-l',
'--list',
dest="update",
action='store_false',
default=False,
help='list server components')
group.add_argument(
'-u',
'--updatepom',
dest="update",
action='store_true',
help='update server components')
parser.add_argument(
'-o',
'--only',
nargs='*',
choices=servers,
help='Space separated list of case sensitive server names to process')
parser.add_argument(
'-s',
'--skip',
nargs='*',
choices=servers,
help='Space separated list of case sensitive server names to exclude from processing')
args = parser.parse_args()
</code></pre>
<p>I like that the choice=servers validates the server names in the input for me, so that I won´t have to. However, having so many valid choices makes the help output look terrible:</p>
<pre class="lang-none prettyprint-override"><code>usage: args.py [-h] [-l | -u]
[-o [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]]
[-s [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]]

A program to update components on servers.

optional arguments:
-h, --help show this help message and exit
-l, --list list server components
-u, --updatepom update server components
-o [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]], --only [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]
Space separated list of case sensitive server names to
process
-s [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]], --skip [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]
Space separated list of case sensitive server names to
exclude from processing
</code></pre>
<p>Which way would you recommend if I want:</p>
<ul>
<li>Nice (mostly) auto-generated help output</li>
<li>Validation that the entries given to the -o or the -s options are in <code>servers</code>.</li>
</ul>
<p>Bonus:</p>
<ul>
<li>Would it be possible to have case insensitive string matching for the server names?</li>
</ul>
<p><strong>Append</strong></p>
<p>I tried using <a href="https://stackoverflow.com/a/9366557">michaelfilms suggestion</a> where the <code>-o</code> <code>-s</code> options are removed from the above output and this part is added:</p>
<pre><code>server optional arguments:
Valid server names are: ApaServer, BananServer, GulServer, SolServer,
RymdServer, SkeppServer, HavsServer, PiratServer, SvartServer,
NattServer, SovServer
</code></pre>
<p>I think it looks pretty good, but I really need to provide help for <code>-o</code> and <code>-s</code> options as the user wouldn´t know about them otherwise. So I am not all the way there yet using this approach.</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top