Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subcommand help should show parent command name in synopsis #287

Closed
remkop opened this issue Feb 14, 2018 · 2 comments
Closed

Subcommand help should show parent command name in synopsis #287

remkop opened this issue Feb 14, 2018 · 2 comments

Comments

@remkop
Copy link
Owner

remkop commented Feb 14, 2018

For example, the JFrog CLI has an rt subcommand, which itself has a number of subcommands.

Executing

jfrog rt dl --help

Gives

Name:
        jfrog rt download - Download files.

Usage:
        jfrog rt dl [command options] <source pattern> [target pattern]
        jfrog rt dl --spec=<File Spec path> [command options]
...

Note that the synopsis shows not just the download subcommand for which help was requested, but the full "path" jfrog rt dl. This should be the default behaviour for picocli.

Another example:

{ ~ }  » jfrog bt -h                                                                                                                                               ~
NAME:
   jfrog bt - Bintray commands

USAGE:
   jfrog bt command [command options][arguments...]

COMMANDS:
   config, c                    Configure Bintray details.
   upload, u                    Upload files.
   download-file, dlf           Download file.
   download-ver, dlv            Download Version files.
   package-show, ps             Show Package details.
   package-create, pc           Create Package.
   package-update, pu           Update Package.
   package-delete, pd           Delete Package.
   version-show, vs             Show Version.
   version-create, vc           Create Version.
   version-update, vu           Update Version.
   version-delete, vd           Delete Version.
   version-publish, vp          Publish Version.
   entitlements, ent            Manage Entitlements.
   access-keys, acc-keys        Manage Access Keys.
   url-sign, us                 Create Signed Download URL.
   gpg-sign-file, gsf           GPG Sign file.
   gpg-sign-ver, gsv            GPG Sign Version.
   logs, l                      Download available log files for a package.
   stream, st                   Open events notification channel.
   help, h                      Shows a list of commands or help for one command

And

{ ~ }  » jfrog bt config -h                                                                                                                                        ~

Name:
        jfrog bt config - Configure Bintray details.

Usage:
        jfrog bt c [command options]
        jfrog bt c show
        jfrog bt c clear

Options:
        --interactive   [Default: true] Set to false if you do not want the config command to be interactive.
        --user          [Optional] Bintray username. If not set, the subject sent as part of the command argument is used for authentication.
        --key           [Mandatory] Bintray API key
        --licenses      [Optional] Default package licenses in the form of Apache-2.0,GPL-3.0...
@remkop remkop added this to the 3.0 milestone Feb 14, 2018
@remkop
Copy link
Owner Author

remkop commented Mar 2, 2018

Index: src/main/java/picocli/CommandLine.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/picocli/CommandLine.java	(revision )
+++ src/main/java/picocli/CommandLine.java	(revision )
@@ -2432,6 +2432,14 @@
              * {@link #DEFAULT_COMMAND_NAME} by default, initialized from {@link Command#name()} if defined. */
             public String name() { return (name == null) ? DEFAULT_COMMAND_NAME : name; }
 
+            /** Returns the fully qualified command name of this subcommand; the command name, prefixed by the names of its parent commands. */
+            public String fullName() {
+                CommandSpec spec = this;
+                StringBuilder sb = new StringBuilder();
+                do { sb.insert(0, ' ').insert(1, spec.name()); spec = spec.parent; } while (spec != null);
+                return sb.toString().trim();
+            }
+
             /** Returns version information for this command, to print to the console when the user specifies an
              * {@linkplain OptionSpec#versionHelp() option} to request version help. This is not part of the usage help message.
              * @return the version strings generated by the {@link #versionProvider() version provider} if one is set, otherwise the {@linkplain #version(String...) version literals}*/
@@ -5061,7 +5069,7 @@
                     sb.append(' ').append(parameterLabelRenderer().renderParameterLabel(positionalParam, ansi(), colorScheme.parameterStyles));
                 }
             }
-            return colorScheme.commandText(commandSpec.name()).toString()
+            return colorScheme.commandText(commandSpec.fullName()).toString()
                     + (sb.toString()) + System.getProperty("line.separator");
         }
         /** Generates a detailed synopsis message showing all options and parameters. Follows the unix convention of
@@ -5136,7 +5144,7 @@
                 }
             }
             // Fix for #142: first line of synopsis overshoots max. characters
-            String commandName = commandSpec.name();
+            String commandName = commandSpec.fullName();
             int firstColumnLength = commandName.length() + synopsisHeadingLength;
 
             // synopsis heading ("Usage: ") may be on the same line, so adjust column width

@remkop remkop modified the milestones: 3.0, 3.1 Apr 30, 2018
@remkop
Copy link
Owner Author

remkop commented May 14, 2018

Index: src/main/java/picocli/CommandLine.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/picocli/CommandLine.java	(revision )
+++ src/main/java/picocli/CommandLine.java	(revision )
@@ -2904,10 +2904,21 @@
              * each {@code UnmatchedArgsBinding} captures the arguments that could not be matched to any options or positional parameters. */
             public List<UnmatchedArgsBinding> unmatchedArgsBindings() { return Collections.unmodifiableList(unmatchedArgs); }
     
-            /** Returns the String to use as the program name in the synopsis line of the help message.
-             * {@link #DEFAULT_COMMAND_NAME} by default, initialized from {@link Command#name()} if defined. */
+            /** Returns name of this command. Used in the synopsis line of the help message.
+             * {@link #DEFAULT_COMMAND_NAME} by default, initialized from {@link Command#name()} if defined.
+             * @see #qualifiedName() */
             public String name() { return (name == null) ? DEFAULT_COMMAND_NAME : name; }
 
+            /** Returns the String to use as the program name in the synopsis line of the help message:
+             * this command's {@link #name() name}, preceded by the qualified name of the parent command, if any.
+             * {@link #DEFAULT_COMMAND_NAME} by default, initialized from {@link Command#name()} if defined.
+             * @since 3.0.1 */
+            public String qualifiedName() {
+                String result = name();
+                if (parent() != null) { result = parent().qualifiedName() + " " + result; }
+                return result;
+            }
+
             /** Returns version information for this command, to print to the console when the user specifies an
              * {@linkplain OptionSpec#versionHelp() option} to request version help. This is not part of the usage help message.
              * @return the version strings generated by the {@link #versionProvider() version provider} if one is set, otherwise the {@linkplain #version(String...) version literals}*/
@@ -6070,7 +6081,7 @@
                 sb.append(" [COMMAND]");
             }
 
-            return colorScheme.commandText(commandSpec.name()).toString()
+            return colorScheme.commandText(commandSpec.qualifiedName()).toString()
                     + (sb.toString()) + System.getProperty("line.separator");
         }
         /** Generates a detailed synopsis message showing all options and parameters. Follows the unix convention of
@@ -6152,7 +6163,7 @@
             }
 
             // Fix for #142: first line of synopsis overshoots max. characters
-            String commandName = commandSpec.name();
+            String commandName = commandSpec.qualifiedName();
             int firstColumnLength = commandName.length() + synopsisHeadingLength;
 
             // synopsis heading ("Usage: ") may be on the same line, so adjust column width

@remkop remkop modified the milestones: 3.1, 3.0.1 May 14, 2018
@remkop remkop closed this as completed in 3eea831 May 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant