python - argparse argument dependency -


If I call the script below with these options:

  - User U 1 - Password P1 - FU F1 - User U2 - User U3 - Password P3  

will print then:

  Namespace (foo = ['Question: What is it? Question: What is it? I in some way to establish a dependency between the user and the password for me, then it throws an error, because the user U2 The password is not specified?  

Less relevant questions: How do I specify the default FU value for all users? With the given input I get foo equal to ['f1', 'bar', 'bar']

One solution to my main question is to see that the list of users and passwords is the same length, but this is not enough to see what I am looking for.

Here is the script :

  import argparse parser = argparse.ArgumentParser () group = parser.add_argument_group (Group.add) _argument ('- user', action = 'append', required = true) group.add_argument ('- password', action = 'append', required = true) group.add_argument ('- -foo', action = 'append ', Default = [' bar ']) Print (parser.pre_arange ())  

In your case, since the options should always be specified simultaneously, or none of them, you can include them in a unique - user-and-password option nargs = 2 logic using 2 It is very easy to handle the values.

Actually you want to be able to provide many couples, but when the first option is found, then is required = true is satisfied, so what do you want in your settings It is too much waste to check it out.

Another way to do this is to use a custom action, for example:

  The class to be imported UserAction (argparse.Action): def __call __ (self, Parser, namespace, value, option_string = none): if len (namespace.passwords) & lt; Len (namespace.users): parser. Terror ('unavailable password') Other: Name location. Keywords: append (value) class password action (argparse.Action): Diff __Coll __ (self, parser, namespace, value, option_string = none): if len (namespace. Users) & lt; = Len (namespace.passwords): parser. Terror ('absentee user') Other: Namespace. password. Append (values) parser = argparse.ArgumentParser () parser.add_argument ('- password', dest = 'password', default = [], verb = password action, required = true) parser.add_argument ('- user', dest = 'Users', default = [], verb = UserAction, required = true) is printed as a print (parser.from);  

:

< Pre> $ python3 ./test_argparse.py - User 1 - password 2 - password 2 - user 3 - password 3 use: test_argparse.py [-h] - password password - user user test_argparse.py: error: user Is absent

and:

  $ python3 ./test_argparse.py - Perspective 1 - password 2 - user 2 - user 3 - compass sword 3 usage: test_argparse.py [-h] - password password - user usa test_argparse.py: error: absent password  
< P> (Note that for this solution - user needs before - password , otherwise the length of the lists does not provide enough information, when there is no alternative .)

Use the last solution simply action = 'append' and end the value of values. Test the magazines. However it can allow things like - User A - User B - Password A - password B which you may or may not want to allow.


Comments