1

I'm developing a simple project with the purpose or learning Python, actually I have version 3.6 and I wanted to build a command line tool to generate password with specific criteria. So fare here is what I got:

import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("-a", "-auto", help="auto mode", action="store_true")
group.add_argument("-m", "-manual", help="manual mode", action="store_true")
parser.parse_args() 

the dead end is I have no idea how to limit a command, example -l -lenght to the reqisite of having -m another stop is, how do I define -l so that it can accept a value, for example -l:8 to specify a password of 8 characters, I also want to put a range limit on -l values, example -l:8-256 and finally, I dont understand the difference between - and -- when defining the parameters.

I have already done all the part of generating passwords, just need a way to control its flow from outside so implementing parameters looked like a good way of doing this.

2 Answers 2

2

What you are looking for is the choices option.

add_argument('--length', type=int, choices=range(8,257)

This will only accept integer values between 8 and 256 (inclusive).

As for your second question, - indicates the shorthand for an option, which is common in most CLI tools, well -- indicates the long hand. It is common practice is CLI tools to provide both a long and a short hand for options.

Sign up to request clarification or add additional context in comments.

7 Comments

how do I bing a parameter to only be used with another paramenter, say for example I can specify -l only when it is run -m while in auto modo I cannot do that and will not accept the lenght value.
That is best done in post-processing, after you have parsed all the options.
To may knowledge you cannot do that with argparse, but that is a very easy if statement that can be done after your arguments have been parsed
Note that your usage message will show all 240+ values if you use choices like this.
@chepner an easy way around this is to change the usage with the metavar option. Checkout stackoverflow.com/questions/25295487/… for an explanation
|
1

You can define a custom type to check for valid values:

from argparse import ArgumentTypeError

def passwd_len(s):
    try:
        s = int(s)
    except ValueError:
        raise ArgumentTypeError("'%s' is not an integer" % (s,))

    if not (8 <= s <= 256):
        raise ArgumentTypeError("%d is not between 8 and 256, inclusive" % (s,))

    return s

parser.add_argument("--length", "-l", type=passwd_len)

The difference between -- and - is one of conventions. Historically, options were single-letter arguments prefixed with a -, and groups of options could be combined: -ab was the same as -a -b. In order to support that convention and allow longer option names, a new convention of using -- to prefix multi-character names was devised. --length is a single option, not a group of six options -l, -e, -n, -g, -t, and -h. Long options cannot be grouped.

I would define the -a and -m options like this:

group = parser.add_mutually_exclusive_group()
group.add_argument("-a", "--auto", help="auto mode", action="store_const", const="auto", dest="mode")
group.add_argument("-m", "--manual", help="manual mode", action="store_const", const="manual", dest="mode")
group.add_argument("--mode", choices=["auto", "manual"])

Now instead of having two Boolean attributes that can never have the same value, you have just one attribute whose value you can check directly. Think of --mode as being the canonical way of choosing a mode, with -a and -m as shortcuts for selecting a specific mode.

2 Comments

what is the advantage of havind a third attribute named --mode respect using alternatively only -a or -m?
You could drop that option completely, and still have -a and -m as the only way to set the mode destination. I threw it in as sort of an afterthought. (You could think of it as demonstrating just how flexible you can be in setting a single destination.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.