@@ -3,26 +3,65 @@ package common
33import (
44 "context"
55 "fmt"
6+ "strings"
67
78 flag "github.com/spf13/pflag"
89
910 consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
1011)
1112
1213var (
13- selectedHeight int64
14- force bool
15- answerYes bool
14+ // HeightFlag is the flag for specifying block height.
15+ HeightFlag * flag.FlagSet
16+
17+ // ForceFlag is a force mode switch.
18+ ForceFlag * flag.FlagSet
19+
20+ // AnswerYesFlag answers yes to all questions.
21+ AnswerYesFlag * flag.FlagSet
22+
23+ // FormatFlag specifies the command's output format (text/json).
24+ FormatFlag * flag.FlagSet
1625)
1726
18- // HeightFlag is the flag for specifying block height .
19- var HeightFlag * flag. FlagSet
27+ // FormatType specifies the type of format for output of commands .
28+ type FormatType string
2029
21- // ForceFlag is a force mode switch.
22- var ForceFlag * flag.FlagSet
30+ // Supported output formats for the format flag.
31+ const (
32+ // Output plain text.
33+ FormatText FormatType = "text"
34+ // Output JSON.
35+ FormatJSON FormatType = "json"
36+ )
37+
38+ // String returns a string representation of the output format type.
39+ func (f * FormatType ) String () string {
40+ return string (* f )
41+ }
2342
24- // AnswerYesFlag answers yes to all questions.
25- var AnswerYesFlag * flag.FlagSet
43+ // Set sets the value of the type to the argument given.
44+ func (f * FormatType ) Set (v string ) error {
45+ switch strings .ToLower (v ) {
46+ case string (FormatText ), string (FormatJSON ):
47+ * f = FormatType (v )
48+ return nil
49+ default :
50+ return fmt .Errorf ("unknown output format type, must be one of: %s" , strings .Join ([]string {string (FormatText ), string (FormatJSON )}, ", " ))
51+ }
52+ }
53+
54+ // Type returns the type of the flag.
55+ func (f * FormatType ) Type () string {
56+ return "FormatType"
57+ }
58+
59+ var (
60+ selectedHeight int64
61+ force bool
62+ answerYes bool
63+ outputFormat = FormatText
64+ )
2665
2766// GetHeight returns the user-selected block height.
2867func GetHeight () int64 {
@@ -34,11 +73,17 @@ func IsForce() bool {
3473 return force
3574}
3675
37- // IsForce returns force mode.
76+ // GetAnswerYes returns whether all interactive questions should be answered
77+ // with yes.
3878func GetAnswerYes () bool {
3979 return answerYes
4080}
4181
82+ // OutputFormat returns the format of the command's output.
83+ func OutputFormat () FormatType {
84+ return outputFormat
85+ }
86+
4287// GetActualHeight returns the user-selected block height if explicitly
4388// specified, or the current latest height.
4489func GetActualHeight (
@@ -65,4 +110,7 @@ func init() {
65110
66111 AnswerYesFlag = flag .NewFlagSet ("" , flag .ContinueOnError )
67112 AnswerYesFlag .BoolVarP (& answerYes , "yes" , "y" , false , "answer yes to all questions" )
113+
114+ FormatFlag = flag .NewFlagSet ("" , flag .ContinueOnError )
115+ FormatFlag .Var (& outputFormat , "format" , "output format [" + strings .Join ([]string {string (FormatText ), string (FormatJSON )}, "," )+ "]" )
68116}
0 commit comments