[PATCH] D83639: [OptTable] Support grouped short options

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 12 00:21:42 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, jhenderson, ikudrin, rupprecht.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

POSIX.1-2017 12.2 Utility Syntax Guidelines, Guideline 5 says:

> One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when grouped behind one '-' delimiter.

i.e. -abc represents -a -b -c. The grouped short options are very common.  Many
utilities extend the syntax by allowing (an option with an argument) following a
sequence of short options.

This patch adds the support to OptTable, similar to cl::Group for CommandLine
(D58711 <https://reviews.llvm.org/D58711>).  llvm-symbolizer will use the feature (D83530 <https://reviews.llvm.org/D83530>). CommandLine is exotic
in some aspects. OptTable is preferred if the user wants to get rid of the
behaviors.

- `cl::opt<bool> i(...)` can be disabled via -i=false or -i=0, which is different from conventional --no-i.
- Handling --foo & --no-foo requires a comparison of argument positions, which is a bit clumsy in user code.

OptTable::parseOneArg (non-const reference InputArgList) is added along with
ParseOneArg (const ArgList &). The duplicate does not look great at first
glance. However, The implementation can be simpler if ArgList is mutable.
(ParseOneArg is used by clang-cl (FlagsToInclude/FlagsToExclude) and lld COFF
(case-insensitive). Adding grouped short options can make the function even more
complex.)

The implementation allows a long option following a group of short options. We
probably should refine the code to disallow this in the future. Allowing this
seems benign for now.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83639

Files:
  llvm/include/llvm/Option/ArgList.h
  llvm/include/llvm/Option/OptTable.h
  llvm/include/llvm/Option/Option.h
  llvm/lib/Option/OptTable.cpp
  llvm/lib/Option/Option.cpp
  llvm/unittests/Option/OptionParsingTest.cpp
  llvm/unittests/Option/Opts.td

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83639.277275.patch
Type: text/x-patch
Size: 8653 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200712/972e53c0/attachment.bin>


More information about the llvm-commits mailing list