@@ -51,16 +51,21 @@ var (
5151
5252// NewCommand created a new `upload` command
5353func NewCommand () * cobra.Command {
54+ uploadFields := map [string ]string {}
5455 uploadCommand := & cobra.Command {
55- Use : "upload" ,
56- Short : tr ("Upload Arduino sketches." ),
57- Long : tr ("Upload Arduino sketches. This does NOT compile the sketch prior to upload." ),
58- Example : " " + os .Args [0 ] + " upload /home/user/Arduino/MySketch" ,
59- Args : cobra .MaximumNArgs (1 ),
56+ Use : "upload" ,
57+ Short : tr ("Upload Arduino sketches." ),
58+ Long : tr ("Upload Arduino sketches. This does NOT compile the sketch prior to upload." ),
59+ Example : "" +
60+ " " + os .Args [0 ] + " upload /home/user/Arduino/MySketch -p /dev/ttyACM0 -b arduino:avr:uno\n " +
61+ " " + os .Args [0 ] + " upload -p 192.168.10.1 -b arduino:avr:uno --upload-field password=abc" ,
62+ Args : cobra .MaximumNArgs (1 ),
6063 PreRun : func (cmd * cobra.Command , args []string ) {
6164 arguments .CheckFlagsConflicts (cmd , "input-file" , "input-dir" )
6265 },
63- Run : runUploadCommand ,
66+ Run : func (cmd * cobra.Command , args []string ) {
67+ runUploadCommand (args , uploadFields )
68+ },
6469 }
6570
6671 fqbnArg .AddToCommand (uploadCommand )
@@ -73,10 +78,11 @@ func NewCommand() *cobra.Command {
7378 programmer .AddToCommand (uploadCommand )
7479 uploadCommand .Flags ().BoolVar (& dryRun , "dry-run" , false , tr ("Do not perform the actual upload, just log out actions" ))
7580 uploadCommand .Flags ().MarkHidden ("dry-run" )
81+ arguments .AddKeyValuePFlag (uploadCommand , & uploadFields , "upload-field" , "F" , nil , tr ("Set a value for a field required to upload." ))
7682 return uploadCommand
7783}
7884
79- func runUploadCommand (command * cobra. Command , args [ ]string ) {
85+ func runUploadCommand (args [] string , uploadFieldsArgs map [ string ]string ) {
8086 logrus .Info ("Executing `arduino-cli upload`" )
8187
8288 path := ""
@@ -147,12 +153,24 @@ func runUploadCommand(command *cobra.Command, args []string) {
147153
148154 fields := map [string ]string {}
149155 if len (userFieldRes .UserFields ) > 0 {
150- feedback .Print (tr ("Uploading to specified board using %s protocol requires the following info:" , port .Protocol ))
151- if f , err := arguments .AskForUserFields (userFieldRes .UserFields ); err != nil {
152- msg := fmt .Sprintf ("%s: %s" , tr ("Error getting user input" ), err )
153- feedback .Fatal (msg , feedback .ErrGeneric )
156+ if len (uploadFieldsArgs ) > 0 {
157+ // If the user has specified some fields via cmd-line, we don't ask for them
158+ for _ , field := range userFieldRes .UserFields {
159+ if value , ok := uploadFieldsArgs [field .Name ]; ok {
160+ fields [field .Name ] = value
161+ } else {
162+ feedback .Fatal (tr ("Missing required upload field: %s" , field .Name ), feedback .ErrBadArgument )
163+ }
164+ }
154165 } else {
155- fields = f
166+ // Otherwise prompt the user for them
167+ feedback .Print (tr ("Uploading to specified board using %s protocol requires the following info:" , port .Protocol ))
168+ if f , err := arguments .AskForUserFields (userFieldRes .UserFields ); err != nil {
169+ msg := fmt .Sprintf ("%s: %s" , tr ("Error getting user input" ), err )
170+ feedback .Fatal (msg , feedback .ErrGeneric )
171+ } else {
172+ fields = f
173+ }
156174 }
157175 }
158176
0 commit comments