This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Don't print "Command Options Usage:" for an alias with no options
ClosedPublic

Authored by DavidSpickett on Jan 11 2022, 3:09 AM.

Details

Summary

"shell" is an alias to "platform shell -h --". Previously you would get this
help text:

(lldb) help shell
Run a shell command on the host. Expects 'raw' input (see 'help raw-input'.)

Syntax: shell <shell-command>

Command Options Usage:

'shell' is an abbreviation for 'platform shell -h --'

Since the code doesn't handle the base command having options
but the alias removing them. With these changes you get:

(lldb) help shell
Run a shell command on the host. Expects 'raw' input (see 'help raw-input'.)

Syntax: shell <shell-command>

'shell' is an abbreviation for 'platform shell -h --'

Note that we already handle a non-alias command having no options,
for example "quit":

(lldb) help quit
Quit the LLDB debugger.

Syntax: quit [exit-code]

Diff Detail

Event Timeline

DavidSpickett created this revision.Jan 11 2022, 3:09 AM
DavidSpickett requested review of this revision.Jan 11 2022, 3:09 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 11 2022, 3:09 AM
This revision is now accepted and ready to land.Jan 11 2022, 11:43 AM
jingham accepted this revision.EditedJan 11 2022, 12:20 PM
jingham added a subscriber: jingham.

I was confused by your example of the "quit" command since the previous code output the banner before checking the NumCommandOptions, so that shouldn't have worked. Actually, there's a check in CommandObject::GenerateHelp for a null return from GetOptions before this GenerateOptionUsage gets called, which bypasses the call. Anyway, with this change we'll do this correctly even if we get here in some other way, so that's all good.

I also think this function is pretty confusing to read. First off, only_print_args is a really confusing name for "is_dash_dash_command", which is what that bool really signifies. Also, we do a bunch of checking for both only_print_args and !only_print_args inside the first "if (!only_print_args)" block, but that variable is a const so that's just weird.

So far as I can see, the only thing we do in the only_print_args == true case is this block:

if (cmd && (only_print_args || cmd->WantsRawCommandString()) &&
    arguments_str.GetSize() > 0) {
  if (!only_print_args)
    strm.PutChar('\n');
  strm.Indent(name);
  strm << " " << arguments_str.GetString();
}

plus resetting the indentation.

This function would be much easier to read if we handle IsDashDash commands as an early return, emitting the command name and arguments, and returning. Then we could remove both of the if (!only_print_args) blocks, and the weird checks inside those blocks, and this would make more sense. That little snippet is sufficiently obvious I don't think repeating it would be a big deal.

OTOH, you didn't originally write this code, so if you don't have time to clean it up further, then this LGTM.

I also think this function is pretty confusing to read.

Enough that I decided not to attempt to unpick it for this bug! I also wondered how the quit command actually worked so thanks for solving that mystery.

I'll go with this as is but if I get a chance I'll try to simplify things like you suggested.