Re: argsparse: allowing --version without mandatory options

2025-10-30 Thread 2QdxY4RzWzUUiLuE
On 2025-10-30 at 13:50:42 +0100,
Loris Bennett  wrote:

> "Loris Bennett"  writes:

> > I am writing a program for the command-line which uses 'argsparse'.  I
> > want to make some options mandatory by setting 'required=True', but
> > still allow the program to run with the option '--version' (which just
> > shows the version and then exits) even if the mandatory options are
> > missing.
> >
> > Is there a standard way of doing this?

Don't take this the wrong way, but what, exactly, is a mandatory option?
Mandatory and optional are quite opposite.  If you have mandatory
arguments, then make the arguments mandatory.  If you have optional
options, then make the options optional.  Otherwise, as a user, I just
have extra typing (i.e., the "--whatever" before the mandatory argument)
to type (redundancy intended).

Elsewhere, I've seen --version and --help options handled as special
cases that just do their thing in their "action" logic and exit the
program right then and there.  I'm not necessarily in favor of such a
design, but it's not as uncommon as I would prefer, and it does meet
your requirement.

HTH.
-- 
https://mail.python.org/mailman3//lists/python-list.python.org


Re: argsparse: allowing --version without mandatory options

2025-10-30 Thread Loris Bennett
"Loris Bennett"  writes:

> Hi,
>
> I am writing a program for the command-line which uses 'argsparse'.  I
> want to make some options mandatory by setting 'required=True', but
> still allow the program to run with the option '--version' (which just
> shows the version and then exits) even if the mandatory options are
> missing.
>
> Is there a standard way of doing this?

I see that instead of setting required to 'True' I can use an expression
which checks whether '--version' has been given:

parser.add_argument(
"uid", nargs="+",
help="UIDs to send mail to"
)
parser.add_argument(
'-s', '--subject', dest='subject', required=not '--version' in sys.argv,
help="subject of email"
)
parser.add_argument(
'-c', '--content_file', dest='content_file', required=not '--version' 
in sys.argv,
help="file containing mail contents (without salutation or signature)"
)
parser.add_argument(
'--version', action='store_true', dest='version',
help="print version"

However, with the above, I still need to specify the mandatory argument,
even if I only want to see the version.

I guess I'll just have to change 

  nargs="+" 

to 

  nargs="*"

and check explicitly whether any arguments have been given.

Seems odd to me though that there is not a standard way of implementing
'--version' which works like '--help' does.

Cheers,

Loris

-- 
This signature is currently under constuction.
-- 
https://mail.python.org/mailman3//lists/python-list.python.org


argsparse: allowing --version without mandatory options

2025-10-30 Thread Loris Bennett
Hi,

I am writing a program for the command-line which uses 'argsparse'.  I
want to make some options mandatory by setting 'required=True', but
still allow the program to run with the option '--version' (which just
shows the version and then exits) even if the mandatory options are
missing.

Is there a standard way of doing this?

Cheers,

Loris 

-- 
This signature is currently under constuction.
-- 
https://mail.python.org/mailman3//lists/python-list.python.org


Re: argsparse: allowing --version without mandatory options

2025-10-30 Thread Mats Wichmann

On 10/30/25 11:47, [email protected] wrote:

On 2025-10-30 at 13:50:42 +0100,



Don't take this the wrong way, but what, exactly, is a mandatory option?
Perhaps an argument that must be given in order to specify a value that 
has no default (not that I'd do that, personally).


Or a word selecting a subcommand, where you *must* specify a command. 
Here you usually fall back to help if not given.


And, yes, --version already gets special handling in argparse, if you 
use the 'version' action.


--
https://mail.python.org/mailman3//lists/python-list.python.org