]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ukify: avoid treating invalid option as an argument 36389/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 14 Feb 2025 10:16:03 +0000 (11:16 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 5 Mar 2025 10:17:33 +0000 (11:17 +0100)
For some reason, argparse treats undefined options as positional args in
certain scenarios:

$ src/ukify/ukify.py --badopt='11'
ukify.py: error: unrecognized arguments: --badopt=11
$ src/ukify/ukify.py --badopt '11'
ukify.py: error: unrecognized arguments: --badopt
$ src/ukify/ukify.py --badopt '11 12'
Assuming obsolete command line syntax with no verb. Please use 'build'.
Traceback (most recent call last):
  File "/home/zbyszek/src/systemd/src/ukify/ukify.py", line 2497, in <module>
    main()
    ~~~~^^
  File "/home/zbyszek/src/systemd/src/ukify/ukify.py", line 2485, in main
    check_inputs(opts)
    ~~~~~~~~~~~~^^^^^^
  File "/home/zbyszek/src/systemd/src/ukify/ukify.py", line 671, in check_inputs
    value.open().close()
    ~~~~~~~~~~^^
  File "/usr/lib64/python3.13/pathlib/_local.py", line 537, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '--badopt=11 12'

I suspect that this is some crap compat for Windows, where option parsing is
an even bigger mess than here.

Being told about positional args, when no positional args were specified is
confusing, so add a check for this.

src/ukify/ukify.py

index fb3912f9021930bd1ee20e27279523f08acbf590..87af8d7864f9678999cef4c66007fec878564e73 100755 (executable)
@@ -2485,6 +2485,12 @@ def finalize_options(opts: argparse.Namespace) -> None:
 
 def parse_args(args: Optional[list[str]] = None) -> argparse.Namespace:
     opts = create_parser().parse_args(args)
+
+    # argparse puts some unknown options in opts.positional. Make sure we don't
+    # try to interpret something that is an option as a positional argument.
+    if any((bad_opt := o).startswith('-') for o in opts.positional):
+        raise ValueError(f'Unknown option: {bad_opt.partition("=")[0]}')
+
     apply_config(opts)
     finalize_options(opts)
     return opts