Refactor of #7887

Closes #7887

Thanks @rhatdan for most of the work

@rhatdan I changed behavior for `CmdLogin`, `CmdLogout`, `CmdPs`. Can you double check it's correct?
This seems a little complicated, although I can see why you did it this way. Maybe there's a good reason for the way things are today, but aren't we before off exposing `FlagSet.name` for this purpose?