Skip to content

Commit 288494c

Browse files
committed
Factored --show-properties cli flag parser
1 parent 581cf30 commit 288494c

File tree

2 files changed

+92
-53
lines changed

2 files changed

+92
-53
lines changed

Diff for: internal/cli/arguments/show_properties.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package arguments
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// ShowProperties represents the --show-properties flag.
25+
type ShowProperties struct {
26+
arg string
27+
}
28+
29+
// ShowPropertiesMode represents the possible values of the --show-properties flag.
30+
type ShowPropertiesMode int
31+
32+
const (
33+
// ShowPropertiesDisabled means that the --show-properties flag has not been used
34+
ShowPropertiesDisabled ShowPropertiesMode = iota
35+
// ShowPropertiesUnexpanded means that the --show-properties flag has been used without a value or with the value "unexpanded"
36+
ShowPropertiesUnexpanded
37+
// ShowPropertiesExpanded means that the --show-properties flag has been used with the value "expanded"
38+
ShowPropertiesExpanded
39+
)
40+
41+
// ParseShowPropertiesArg parses the --show-properties flag value and returns the corresponding ShowProperties value.
42+
func (p *ShowProperties) Get() (ShowPropertiesMode, error) {
43+
switch p.arg {
44+
case "disabled":
45+
return ShowPropertiesDisabled, nil
46+
case "unexpanded":
47+
return ShowPropertiesUnexpanded, nil
48+
case "expanded":
49+
return ShowPropertiesExpanded, nil
50+
default:
51+
return ShowPropertiesDisabled, fmt.Errorf(tr("invalid option '%s'.", p.arg))
52+
}
53+
}
54+
55+
// AddToCommand adds the --show-properties flag to the specified command.
56+
func (p *ShowProperties) AddToCommand(command *cobra.Command) {
57+
command.Flags().StringVar(&p.arg,
58+
"show-properties", "disabled",
59+
tr(`Show build properties. The properties are returned exactly as they are defined. Use "--show-properties=expanded" to replace placeholders with context values.`),
60+
)
61+
command.Flags().Lookup("show-properties").NoOptDefVal = "unexpanded" // default if the flag is present with no value
62+
}

Diff for: internal/cli/compile/compile.go

+30-53
Original file line numberDiff line numberDiff line change
@@ -44,50 +44,30 @@ import (
4444
"github.com/spf13/cobra"
4545
)
4646

47-
type showPropertiesMode int
48-
49-
const (
50-
showPropertiesModeDisabled showPropertiesMode = iota
51-
showPropertiesModeUnexpanded
52-
showPropertiesModeExpanded
53-
)
54-
55-
func parseShowPropertiesArg(showProperties string) (showPropertiesMode, error) {
56-
val, ok := map[string]showPropertiesMode{
57-
"disabled": showPropertiesModeDisabled,
58-
"unexpanded": showPropertiesModeUnexpanded,
59-
"expanded": showPropertiesModeExpanded,
60-
}[showProperties]
61-
if !ok {
62-
return showPropertiesModeDisabled, fmt.Errorf(tr("invalid option '%s'.", showProperties))
63-
}
64-
return val, nil
65-
}
66-
6747
var (
68-
fqbnArg arguments.Fqbn // Fully Qualified Board Name, e.g.: arduino:avr:uno.
69-
profileArg arguments.Profile // Profile to use
70-
showPropertiesArg string // Show all build preferences used instead of compiling.
71-
preprocess bool // Print preprocessed code to stdout.
72-
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
73-
buildPath string // Path where to save compiled files.
74-
buildProperties []string // List of custom build properties separated by commas. Or can be used multiple times for multiple properties.
75-
keysKeychain string // The path of the dir where to search for the custom keys to sign and encrypt a binary. Used only by the platforms that supports it
76-
signKey string // The name of the custom signing key to use to sign a binary during the compile process. Used only by the platforms that supports it
77-
encryptKey string // The name of the custom encryption key to use to encrypt a binary during the compile process. Used only by the platforms that supports it
78-
warnings string // Used to tell gcc which warning level to use.
79-
verbose bool // Turns on verbose mode.
80-
quiet bool // Suppresses almost every output.
81-
uploadAfterCompile bool // Upload the binary after the compilation.
82-
portArgs arguments.Port // Upload port, e.g.: COM10 or /dev/ttyACM0.
83-
verify bool // Upload, verify uploaded binary after the upload.
84-
exportDir string // The compiled binary is written to this file
85-
optimizeForDebug bool // Optimize compile output for debug, not for release
86-
programmer arguments.Programmer // Use the specified programmer to upload
87-
clean bool // Cleanup the build folder and do not use any cached build
88-
compilationDatabaseOnly bool // Only create compilation database without actually compiling
89-
sourceOverrides string // Path to a .json file that contains a set of replacements of the sketch source code.
90-
dumpProfile bool // Create and print a profile configuration from the build
48+
fqbnArg arguments.Fqbn // Fully Qualified Board Name, e.g.: arduino:avr:uno.
49+
profileArg arguments.Profile // Profile to use
50+
showPropertiesArg arguments.ShowProperties // Show all build preferences used instead of compiling.
51+
preprocess bool // Print preprocessed code to stdout.
52+
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
53+
buildPath string // Path where to save compiled files.
54+
buildProperties []string // List of custom build properties separated by commas. Or can be used multiple times for multiple properties.
55+
keysKeychain string // The path of the dir where to search for the custom keys to sign and encrypt a binary. Used only by the platforms that supports it
56+
signKey string // The name of the custom signing key to use to sign a binary during the compile process. Used only by the platforms that supports it
57+
encryptKey string // The name of the custom encryption key to use to encrypt a binary during the compile process. Used only by the platforms that supports it
58+
warnings string // Used to tell gcc which warning level to use.
59+
verbose bool // Turns on verbose mode.
60+
quiet bool // Suppresses almost every output.
61+
uploadAfterCompile bool // Upload the binary after the compilation.
62+
portArgs arguments.Port // Upload port, e.g.: COM10 or /dev/ttyACM0.
63+
verify bool // Upload, verify uploaded binary after the upload.
64+
exportDir string // The compiled binary is written to this file
65+
optimizeForDebug bool // Optimize compile output for debug, not for release
66+
programmer arguments.Programmer // Use the specified programmer to upload
67+
clean bool // Cleanup the build folder and do not use any cached build
68+
compilationDatabaseOnly bool // Only create compilation database without actually compiling
69+
sourceOverrides string // Path to a .json file that contains a set of replacements of the sketch source code.
70+
dumpProfile bool // Create and print a profile configuration from the build
9171
// library and libraries sound similar but they're actually different.
9272
// library expects a path to the root folder of one single library.
9373
// libraries expects a path to a directory containing multiple libraries, similarly to the <directories.user>/libraries path.
@@ -115,10 +95,7 @@ func NewCommand() *cobra.Command {
11595
fqbnArg.AddToCommand(compileCommand)
11696
profileArg.AddToCommand(compileCommand)
11797
compileCommand.Flags().BoolVar(&dumpProfile, "dump-profile", false, tr("Create and print a profile configuration from the build."))
118-
compileCommand.Flags().StringVar(&showPropertiesArg, "show-properties", "disabled",
119-
tr(`Show build properties instead of compiling. The properties are returned exactly as they are defined. Use "--show-properties=expanded" to replace placeholders with compilation context values.`),
120-
)
121-
compileCommand.Flags().Lookup("show-properties").NoOptDefVal = "unexpanded" // default if the flag is present with no value
98+
showPropertiesArg.AddToCommand(compileCommand)
12299
compileCommand.Flags().BoolVar(&preprocess, "preprocess", false, tr("Print preprocessed code to stdout instead of compiling."))
123100
compileCommand.Flags().StringVar(&buildCachePath, "build-cache-path", "", tr("Builds of 'core.a' are saved into this path to be cached and reused."))
124101
compileCommand.Flags().StringVarP(&exportDir, "output-dir", "", "", tr("Save build artifacts in this directory."))
@@ -210,14 +187,14 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
210187
overrides = o.Overrides
211188
}
212189

213-
showProperties, err := parseShowPropertiesArg(showPropertiesArg)
190+
showProperties, err := showPropertiesArg.Get()
214191
if err != nil {
215192
feedback.Fatal(tr("Error parsing --show-properties flag: %v", err), feedback.ErrGeneric)
216193
}
217194

218195
var stdOut, stdErr io.Writer
219196
var stdIORes func() *feedback.OutputStreamsResult
220-
if showProperties != showPropertiesModeDisabled {
197+
if showProperties != arguments.ShowPropertiesDisabled {
221198
stdOut, stdErr, stdIORes = feedback.NewBufferedStreams()
222199
} else {
223200
stdOut, stdErr, stdIORes = feedback.OutputStreams()
@@ -235,7 +212,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
235212
Instance: inst,
236213
Fqbn: fqbn,
237214
SketchPath: sketchPath.String(),
238-
ShowProperties: showProperties != showPropertiesModeDisabled,
215+
ShowProperties: showProperties != arguments.ShowPropertiesDisabled,
239216
Preprocess: preprocess,
240217
BuildCachePath: buildCachePath,
241218
BuildPath: buildPath,
@@ -387,7 +364,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
387364
}
388365
feedback.FatalResult(res, feedback.ErrGeneric)
389366
}
390-
if showProperties == showPropertiesModeExpanded {
367+
if showProperties == arguments.ShowPropertiesExpanded {
391368
expandPropertiesInResult(res)
392369
}
393370
feedback.PrintResult(res)
@@ -413,15 +390,15 @@ type compileResult struct {
413390
ProfileOut string `json:"profile_out,omitempty"`
414391
Error string `json:"error,omitempty"`
415392

416-
showPropertiesMode showPropertiesMode
393+
showPropertiesMode arguments.ShowPropertiesMode
417394
}
418395

419396
func (r *compileResult) Data() interface{} {
420397
return r
421398
}
422399

423400
func (r *compileResult) String() string {
424-
if r.showPropertiesMode != showPropertiesModeDisabled {
401+
if r.showPropertiesMode != arguments.ShowPropertiesDisabled {
425402
return strings.Join(r.BuilderResult.GetBuildProperties(), fmt.Sprintln())
426403
}
427404

0 commit comments

Comments
 (0)