Skip to content

Commit 2b85f72

Browse files
authored
Merge pull request #1855 from dearchap/issue_1607
Fix:(issue_1607) Allow exporting command and arguments/options to json
2 parents 7a9aea9 + 5c64b9d commit 2b85f72

File tree

6 files changed

+703
-237
lines changed

6 files changed

+703
-237
lines changed

args.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ type Argument interface {
6767
}
6868

6969
type ArgumentBase[T any, C any, VC ValueCreator[T, C]] struct {
70-
Name string // the name of this argument
71-
Value T // the default value of this argument
72-
Destination *T // the destination point for this argument
73-
Values *[]T // all the values of this argument, only if multiple are supported
74-
UsageText string // the usage text to show
75-
Min int // the min num of occurrences of this argument
76-
Max int // the max num of occurrences of this argument, set to -1 for unlimited
77-
Config C // config for this argument similar to Flag Config
70+
Name string `json:"name"` // the name of this argument
71+
Value T `json:"value"` // the default value of this argument
72+
Destination *T `json:"-"` // the destination point for this argument
73+
Values *[]T `json:"-"` // all the values of this argument, only if multiple are supported
74+
UsageText string `json:"usageText"` // the usage text to show
75+
Min int `json:"minTimes"` // the min num of occurrences of this argument
76+
Max int `json:"maxTimes"` // the max num of occurrences of this argument, set to -1 for unlimited
77+
Config C `json:"config"` // config for this argument similar to Flag Config
7878
}
7979

8080
func (a *ArgumentBase[T, C, VC]) Usage() string {

command.go

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,108 +28,109 @@ type contextKey string
2828
// Command may contain Flags and sub-commands in Commands.
2929
type Command struct {
3030
// The name of the command
31-
Name string
31+
Name string `json:"name"`
3232
// A list of aliases for the command
33-
Aliases []string
33+
Aliases []string `json:"aliases"`
3434
// A short description of the usage of this command
35-
Usage string
35+
Usage string `json:"usage"`
3636
// Text to override the USAGE section of help
37-
UsageText string
37+
UsageText string `json:"usageText"`
3838
// A short description of the arguments of this command
39-
ArgsUsage string
39+
ArgsUsage string `json:"argsUsage"`
4040
// Version of the command
41-
Version string
41+
Version string `json:"version"`
4242
// Longer explanation of how the command works
43-
Description string
43+
Description string `json:"description"`
4444
// DefaultCommand is the (optional) name of a command
4545
// to run if no command names are passed as CLI arguments.
46-
DefaultCommand string
46+
DefaultCommand string `json:"defaultCommand"`
4747
// The category the command is part of
48-
Category string
48+
Category string `json:"category"`
4949
// List of child commands
50-
Commands []*Command
50+
Commands []*Command `json:"commands"`
5151
// List of flags to parse
52-
Flags []Flag
52+
Flags []Flag `json:"flags"`
5353
// Boolean to hide built-in help command and help flag
54-
HideHelp bool
54+
HideHelp bool `json:"hideHelp"`
5555
// Ignored if HideHelp is true.
56-
HideHelpCommand bool
56+
HideHelpCommand bool `json:"hideHelpCommand"`
5757
// Boolean to hide built-in version flag and the VERSION section of help
58-
HideVersion bool
58+
HideVersion bool `json:"hideVersion"`
5959
// Boolean to enable shell completion commands
60-
EnableShellCompletion bool
60+
EnableShellCompletion bool `json:"-"`
6161
// Shell Completion generation command name
62-
ShellCompletionCommandName string
62+
ShellCompletionCommandName string `json:"-"`
6363
// The function to call when checking for shell command completions
64-
ShellComplete ShellCompleteFunc
64+
ShellComplete ShellCompleteFunc `json:"-"`
6565
// An action to execute before any subcommands are run, but after the context is ready
6666
// If a non-nil error is returned, no subcommands are run
67-
Before BeforeFunc
67+
Before BeforeFunc `json:"-"`
6868
// An action to execute after any subcommands are run, but after the subcommand has finished
6969
// It is run even if Action() panics
70-
After AfterFunc
70+
After AfterFunc `json:"-"`
7171
// The function to call when this command is invoked
72-
Action ActionFunc
72+
Action ActionFunc `json:"-"`
7373
// Execute this function if the proper command cannot be found
74-
CommandNotFound CommandNotFoundFunc
74+
CommandNotFound CommandNotFoundFunc `json:"-"`
7575
// Execute this function if a usage error occurs.
76-
OnUsageError OnUsageErrorFunc
76+
OnUsageError OnUsageErrorFunc `json:"-"`
7777
// Execute this function when an invalid flag is accessed from the context
78-
InvalidFlagAccessHandler InvalidFlagAccessFunc
78+
InvalidFlagAccessHandler InvalidFlagAccessFunc `json:"-"`
7979
// Boolean to hide this command from help or completion
80-
Hidden bool
80+
Hidden bool `json:"hidden"`
8181
// List of all authors who contributed (string or fmt.Stringer)
82-
Authors []any // TODO: ~string | fmt.Stringer when interface unions are available
82+
// TODO: ~string | fmt.Stringer when interface unions are available
83+
Authors []any `json:"authors"`
8384
// Copyright of the binary if any
84-
Copyright string
85+
Copyright string `json:"copyright"`
8586
// Reader reader to write input to (useful for tests)
86-
Reader io.Reader
87+
Reader io.Reader `json:"-"`
8788
// Writer writer to write output to
88-
Writer io.Writer
89+
Writer io.Writer `json:"-"`
8990
// ErrWriter writes error output
90-
ErrWriter io.Writer
91+
ErrWriter io.Writer `json:"-"`
9192
// ExitErrHandler processes any error encountered while running an App before
9293
// it is returned to the caller. If no function is provided, HandleExitCoder
9394
// is used as the default behavior.
94-
ExitErrHandler ExitErrHandlerFunc
95+
ExitErrHandler ExitErrHandlerFunc `json:"-"`
9596
// Other custom info
96-
Metadata map[string]interface{}
97+
Metadata map[string]interface{} `json:"metadata"`
9798
// Carries a function which returns app specific info.
98-
ExtraInfo func() map[string]string
99+
ExtraInfo func() map[string]string `json:"-"`
99100
// CustomRootCommandHelpTemplate the text template for app help topic.
100101
// cli.go uses text/template to render templates. You can
101102
// render custom help text by setting this variable.
102-
CustomRootCommandHelpTemplate string
103+
CustomRootCommandHelpTemplate string `json:"-"`
103104
// SliceFlagSeparator is used to customize the separator for SliceFlag, the default is ","
104-
SliceFlagSeparator string
105+
SliceFlagSeparator string `json:"sliceFlagSeparator"`
105106
// DisableSliceFlagSeparator is used to disable SliceFlagSeparator, the default is false
106-
DisableSliceFlagSeparator bool
107+
DisableSliceFlagSeparator bool `json:"disableSliceFlagSeparator"`
107108
// Boolean to enable short-option handling so user can combine several
108109
// single-character bool arguments into one
109110
// i.e. foobar -o -v -> foobar -ov
110-
UseShortOptionHandling bool
111+
UseShortOptionHandling bool `json:"useShortOptionHandling"`
111112
// Enable suggestions for commands and flags
112-
Suggest bool
113+
Suggest bool `json:"suggest"`
113114
// Allows global flags set by libraries which use flag.XXXVar(...) directly
114115
// to be parsed through this library
115-
AllowExtFlags bool
116+
AllowExtFlags bool `json:"allowExtFlags"`
116117
// Treat all flags as normal arguments if true
117-
SkipFlagParsing bool
118+
SkipFlagParsing bool `json:"skipFlagParsing"`
118119
// CustomHelpTemplate the text template for the command help topic.
119120
// cli.go uses text/template to render templates. You can
120121
// render custom help text by setting this variable.
121-
CustomHelpTemplate string
122+
CustomHelpTemplate string `json:"-"`
122123
// Use longest prefix match for commands
123-
PrefixMatchCommands bool
124+
PrefixMatchCommands bool `json:"prefixMatchCommands"`
124125
// Custom suggest command for matching
125-
SuggestCommandFunc SuggestCommandFunc
126+
SuggestCommandFunc SuggestCommandFunc `json:"-"`
126127
// Flag exclusion group
127-
MutuallyExclusiveFlags []MutuallyExclusiveFlags
128+
MutuallyExclusiveFlags []MutuallyExclusiveFlags `json:"mutuallyExclusiveFlags"`
128129
// Arguments to parse for this command
129-
Arguments []Argument
130+
Arguments []Argument `json:"arguments"`
130131
// Whether to read arguments from stdin
131132
// applicable to root command only
132-
ReadArgsFromStdin bool
133+
ReadArgsFromStdin bool `json:"readArgsFromStdin"`
133134

134135
// categories contains the categorized commands and is populated on app startup
135136
categories CommandCategories

0 commit comments

Comments
 (0)