Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
fuchsia.googlesource.com-third_party-github.com-google-subcommands
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fuchsia-mirror
fuchsia.googlesource.com-third_party-github.com-google-subcommands
Commits
709b4d9d
Commit
709b4d9d
authored
9 years ago
by
Akhil Pai
Browse files
Options
Downloads
Patches
Plain Diff
Commander output and error specification support for subcommands.
parent
8c479d77
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
subcommands.go
+14
-9
14 additions, 9 deletions
subcommands.go
with
14 additions
and
9 deletions
subcommands.go
+
14
−
9
View file @
709b4d9d
...
...
@@ -54,6 +54,9 @@ type Commander struct {
topFlags
*
flag
.
FlagSet
// top-level flags
important
[]
string
// important top-level flags
name
string
// normally path.Base(os.Args[0])
Output
io
.
Writer
// Output specifies where the commander should write its output (default: os.Stdout).
Error
io
.
Writer
// Error specifies where the commander should write its error (default: os.Stderr).
}
// A commandGroup represents a set of commands about a common topic.
...
...
@@ -79,8 +82,10 @@ func NewCommander(topLevelFlags *flag.FlagSet, name string) *Commander {
cdr
:=
&
Commander
{
topFlags
:
topLevelFlags
,
name
:
name
,
Output
:
os
.
Stdout
,
Error
:
os
.
Stderr
,
}
topLevelFlags
.
Usage
=
func
()
{
cdr
.
explain
(
os
.
Stder
r
)
}
topLevelFlags
.
Usage
=
func
()
{
cdr
.
explain
(
cdr
.
Erro
r
)
}
return
cdr
}
...
...
@@ -129,7 +134,7 @@ func (cdr *Commander) Execute(ctx context.Context, args ...interface{}) ExitStat
continue
}
f
:=
flag
.
NewFlagSet
(
name
,
flag
.
ContinueOnError
)
f
.
Usage
=
func
()
{
explain
(
os
.
Stder
r
,
cmd
)
}
f
.
Usage
=
func
()
{
explain
(
cdr
.
Erro
r
,
cmd
)
}
cmd
.
SetFlags
(
f
)
if
f
.
Parse
(
cdr
.
topFlags
.
Args
()[
1
:
])
!=
nil
{
return
ExitUsageError
...
...
@@ -225,7 +230,7 @@ func (h *helper) Usage() string {
func
(
h
*
helper
)
Execute
(
_
context
.
Context
,
f
*
flag
.
FlagSet
,
args
...
interface
{})
ExitStatus
{
switch
f
.
NArg
()
{
case
0
:
(
*
Commander
)(
h
)
.
explain
(
os
.
Stdo
ut
)
(
*
Commander
)(
h
)
.
explain
(
h
.
Outp
ut
)
return
ExitSuccess
case
1
:
...
...
@@ -234,11 +239,11 @@ func (h *helper) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}
if
f
.
Arg
(
0
)
!=
cmd
.
Name
()
{
continue
}
explain
(
os
.
Stdo
ut
,
cmd
)
explain
(
h
.
Outp
ut
,
cmd
)
return
ExitSuccess
}
}
fmt
.
Fprintf
(
os
.
Stder
r
,
"Subcommand %s not understood
\n
"
,
f
.
Arg
(
0
))
fmt
.
Fprintf
(
h
.
Erro
r
,
"Subcommand %s not understood
\n
"
,
f
.
Arg
(
0
))
}
f
.
Usage
()
...
...
@@ -272,7 +277,7 @@ func (flg *flagger) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}
if
f
.
NArg
()
==
0
{
if
flg
.
topFlags
==
nil
{
fmt
.
P
rintln
(
"No top-level flags are defined."
)
fmt
.
Fp
rintln
(
flg
.
Output
,
"No top-level flags are defined."
)
}
else
{
flg
.
topFlags
.
PrintDefaults
()
}
...
...
@@ -285,13 +290,13 @@ func (flg *flagger) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}
continue
}
subflags
:=
flag
.
NewFlagSet
(
cmd
.
Name
(),
flag
.
PanicOnError
)
subflags
.
SetOutput
(
os
.
Stdo
ut
)
subflags
.
SetOutput
(
flg
.
Outp
ut
)
cmd
.
SetFlags
(
subflags
)
subflags
.
PrintDefaults
()
return
ExitSuccess
}
}
fmt
.
Fprintf
(
os
.
Stder
r
,
"Subcommand %s not understood
\n
"
,
f
.
Arg
(
0
))
fmt
.
Fprintf
(
flg
.
Erro
r
,
"Subcommand %s not understood
\n
"
,
f
.
Arg
(
0
))
return
ExitFailure
}
...
...
@@ -319,7 +324,7 @@ func (l *lister) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) E
for
_
,
group
:=
range
l
.
commands
{
for
_
,
cmd
:=
range
group
.
commands
{
fmt
.
P
rintf
(
"%s
\n
"
,
cmd
.
Name
())
fmt
.
Fp
rintf
(
l
.
Output
,
"%s
\n
"
,
cmd
.
Name
())
}
}
return
ExitSuccess
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment