Subject: bug#63733: 28.3; error ("Unknown option ‘--batch’")

From: Eli Zaretskii <[email-masked]>
Date: Fri, 26 May 2023 12:10:21 +0300

> From: Ulrich Müller <[email-masked]>
> Date: Fri, 26 May 2023 09:43:40 +0200
> 
> Something is strange with option parsing:
> 
> $ emacs -Q -batch -batch --eval '(message "hello, world")'
> hello, world
> $ emacs -Q --batch --batch --eval '(message "hello, world")'
> hello, world
> $ emacs -Q -batch --batch --eval '(message "hello, world")'
> Debugger entered--Lisp error: (error "Unknown option ‘--batch’")
>   signal(error ("Unknown option ‘--batch’"))
>   error("Unknown option `%s'" "--batch")
>   command-line-1(("--batch" "-Q" "--eval" "(message \"hello, world\")"))
>   command-line()
>   normal-top-level()
> 
> $ emacs -Q --batch -batch --eval '(message "hello, world")'
> Debugger entered--Lisp error: (error "Unknown option ‘-batch’")
>   signal(error ("Unknown option ‘-batch’"))
>   error("Unknown option `%s'" "-batch")
>   command-line-1(("-batch" "-Q" "--eval" "(message \"hello, world\")"))
>   command-line()
>   normal-top-level()
> 
> 
> I would expect -batch/--batch to be idempotent in all four combinations
> above.

They are not idempotent.  Emacs doesn't use getopt, it uses its own
custom code for parsing the command-line options, because our
situation is complex: we only process part of the command-line options
in C, the rest are processed in Lisp.

What happens here is that, when you use identical "-batch" or
"--batch" options several times, the code in sort_args removes the
identical copies, and we are left with a single option to process.
But if you use both "-batch" and "--batch", the extra option is not
removed, and they both appear in the sorted argv[].  Then one of them
is processed by 'main', the other is left for command-line-1, which
doesn't know what to do with it.

I'm not sure we want to support this use case.  It certainly doesn't
make sense to complicate our command-line parsing for its sake.
Perhaps if a simple and safe solution can be found for removing such
"duplicates" in sort_args, we could install such a change, but
definitely not on the emacs-29 release branch.

Thanks.