opts_data 'requires': support non-boolean a-value

This commit is contained in:
The MMGen Project 2026-10-19 09:06:13 +00:00
commit e93aec5a19
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 17 additions and 5 deletions

View file

@ -30,11 +30,13 @@ def die2(exit_val, s):
sys.stderr.write(s+'\n')
sys.exit(exit_val)
def fmt_opt_val(opt_in, val):
def fmt_opt_val(opt_in, val, requires=False):
opt = opt_in.replace('_', '-')
match val:
case True:
return f'--{opt}'
case False if requires:
return f'--{opt} to be omitted'
case False:
return f'--no-{opt}'
case _:
@ -731,10 +733,13 @@ class Config(Lockable):
def _check_opts_data_requires_opts(self, opts_data):
for a_opt, a_val, b_opt, b_val in opts_data['requires']:
if getattr(self, a_opt, False) == a_val and getattr(self, b_opt, False) != b_val:
usr_a_val = getattr(self, a_opt, False)
if (
(usr_a_val == a_val or (usr_a_val and a_val is bool))
and getattr(self, b_opt, False) != b_val):
die('UserOptError', 'Option {} requires option {}\n'.format(
fmt_opt_val(a_opt, a_val),
fmt_opt_val(b_opt, b_val)))
fmt_opt_val(a_opt, usr_a_val),
fmt_opt_val(b_opt, b_val, requires=True)))
def _die_on_incompatible_opts(self):
for group in self._incompatible_opts:

View file

@ -63,6 +63,7 @@ class CmdTestOpts(CmdTestBase):
('opt_bad_sets2', (41, "invalid value for opts_data['sets']", [])),
('opt_bad_requires1', (41, "invalid value for opts_data['requires']", [])),
('opt_bad_requires2', (41, "invalid value for opts_data['requires']", [])),
('opt_bad_requires3', (41, "invalid value for opts_data['requires']", [])),
('opt_invalid_1', (41, 'invalid cmdline opt ‘--x’', [])),
('opt_invalid_2', (41, 'invalid cmdline opt ‘---’', [])),
('opt_invalid_5', (41, 'invalid cmdline opt (missing parameter)', [])),
@ -353,7 +354,10 @@ class CmdTestOpts(CmdTestBase):
return self.do_run(['--coin=DOGE'], 'requires option', 1)
def opt_bad_requires2(self):
return self.do_run(['--no-foobleize', '--silent'], 'requires option --no-', 1)
return self.do_run(['--no-foobleize', '--silent'], 'to be omitted', 1)
def opt_bad_requires3(self):
return self.do_run(['--data-size=123', '--data-ratio=3%'], 'to be omitted', 1)
def opt_invalid(self, args, expect, opts=[], need_proto=False, exit_val=1):
t = self.spawn_prog(args, opts=opts, exit_val=exit_val, need_proto=need_proto)

View file

@ -14,6 +14,7 @@ opts_data = {
'requires': [
('coin', 'DOGE', 'no_foobleize', True),
('no_foobleize', True, 'silent', False),
('data_size', bool, 'data_ratio', False),
],
'text': {
'desc': 'Opts test',
@ -23,6 +24,8 @@ opts_data = {
--, --longhelp Print help message for long (global) options
-i, --in-fmt= f Input is from wallet format 'f'
-d, --outdir= d Use outdir 'd'
--, --data-size=N Specify data size of N
--, --data-ratio=N Specify data ratio of N percent
-C, --print-checksum Print a checksum
-E, --fee-estimate-mode=M Specify the network fee estimate mode.
-F, --no-foobleize Do not foobleize the output, even on user request