[PATCH] D24489: [Support][CommandLine] Add cl::getRegisteredSubcommands()

Dean Michael Berris via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 14 22:49:39 PDT 2016


On Thu, Sep 15, 2016 at 12:45 AM David Blaikie <dblaikie at gmail.com> wrote:

> On Tue, Sep 13, 2016 at 6:37 PM Dean Michael Berris <dberris at google.com>
> wrote:
>
> On Wed, Sep 14, 2016 at 3:13 AM David Blaikie <dblaikie at gmail.com> wrote:
>
> On Mon, Sep 12, 2016 at 6:57 PM Dean Michael Berris <dberris at google.com>
> wrote:
>
> dberris created this revision.
> dberris added reviewers: zturner, dblaikie, echristo.
> dberris added a subscriber: llvm-commits.
> Herald added a subscriber: mehdi_amini.
>
> This should allow users of the library to get a range to iterate through
> all the subcommands that are registered to the global parser. This
> allows users to define subcommands in libraries that self-register to
> have dispatch done at a different stage (like main). It allows for
> writing code like the following:
>
>     for (auto *S : cl::getRegisteredSubcommands()) {
>       if (*S) {
>         // Dispatch on S->getName().
>       }
>     }
>
>
> FWIW: I wasn't expecting/suggesting we should have to compare by strings
> (stringly typed isn't ideal, of course) - I was hoping/figuring we could
> declaer the subcommand in the header (along with the single entry point
> function) and then define it over in the file that defines the single entry
> point along with the subcommand's flags.
>
>
> Unfortunately that doesn't remove the dispatch, which would be manual, as
> opposed to doing this which would be automatic -- i.e. all the
> implementations can register a function to invoke to a global map, which is
> used to dispatch, makes extending easier/simpler than having to manually
> add if statements.
>
>
> Ah, OK - that's more fuss/bother than I was thinking of for a handful of
> things - a switch/if chain in main that was pretty mechanical/repetitive
> didn't seem like a big burden, but I don't mind a registration system if
> you prefer that (it trips into my "potentially overengineering" bucket - so
> if you want to save some time/work, don't feel like I'm pushing you to
> build such a registration system).
>
>
It's actually less work, given that the number of patches I have lined up
already make this cumbersome to do (3 might be fine, but 5 isn't, and
certainly not if we're going to implement more).


> (but mostly it was the suggested string comparison that I was extra
> uncomfortable with here - so long as the registration system used the
> Subcommand + dispatch function, rather than the name + dispatch function,
> I'd be more or less comfortable with it)
>

Yes, the description gives the idea. :) You can certainly use the
subcommand's pointer as the identifier in the registry instead, but that's
just one of the many ways to do it.

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160915/99246d37/attachment.html>


More information about the llvm-commits mailing list