diff --git a/cli/cli_test.go b/cli/cli_test.go index 583201ade5d..3d399f7a956 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -313,7 +313,7 @@ func TestCompileCommandsIntegration(t *testing.T) { // Build sketch without FQBN exitCode, d = executeWithArgs("compile", sketchPath) require.NotZero(t, exitCode) - require.Contains(t, string(d), "required flag(s) \"fqbn\" not set") + require.Contains(t, string(d), "Error: no FQBN provided. Set --fqbn flag or attach board to sketch") // Build sketch for arduino:avr:uno exitCode, d = executeWithArgs("compile", "-b", "arduino:avr:uno", sketchPath) diff --git a/cli/compile/compile.go b/cli/compile/compile.go index 84514062e60..5a2182d6071 100644 --- a/cli/compile/compile.go +++ b/cli/compile/compile.go @@ -21,6 +21,8 @@ import ( "context" "os" + "github.com/arduino/arduino-cli/arduino/sketches" + "github.com/arduino/arduino-cli/cli/feedback" "github.com/arduino/arduino-cli/cli/errorcodes" @@ -80,8 +82,6 @@ func NewCommand() *cobra.Command { command.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.") command.Flags().StringVar(&vidPid, "vid-pid", "", "When specified, VID/PID specific build properties are used, if boards supports them.") - command.MarkFlagRequired("fqbn") - return command } @@ -98,6 +98,15 @@ func run(cmd *cobra.Command, args []string) { } sketchPath := initSketchPath(path) + sketch, err := sketches.NewSketchFromPath(sketchPath) + if err != nil { + feedback.Errorf("Error opening sketch: %v", err) + os.Exit(errorcodes.ErrGeneric) + } + if fqbn == "" && sketch.Metadata.CPU.Fqbn == "" { + feedback.Errorf("Error: no FQBN provided. Set --fqbn flag or attach board to sketch") + os.Exit(errorcodes.ErrGeneric) + } _, err = compile.Compile(context.Background(), &rpc.CompileReq{ Instance: inst, diff --git a/cli/upload/upload.go b/cli/upload/upload.go index 9e536deba82..8558f5052bc 100644 --- a/cli/upload/upload.go +++ b/cli/upload/upload.go @@ -21,6 +21,8 @@ import ( "context" "os" + "github.com/arduino/arduino-cli/arduino/sketches" + "github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/feedback" "github.com/arduino/arduino-cli/cli/instance" @@ -56,7 +58,6 @@ func NewCommand() *cobra.Command { uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.") uploadCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, "Optional, turns on verbose mode.") - uploadCommand.MarkFlagRequired("fqbn") uploadCommand.MarkFlagRequired("port") return uploadCommand @@ -74,8 +75,17 @@ func run(command *cobra.Command, args []string) { path = paths.New(args[0]) } sketchPath := initSketchPath(path) + sketch, err := sketches.NewSketchFromPath(sketchPath) + if err != nil { + feedback.Errorf("Error opening sketch: %v", err) + os.Exit(errorcodes.ErrGeneric) + } + if fqbn == "" && sketch.Metadata.CPU.Fqbn == "" { + feedback.Errorf("Error: no FQBN provided. Set --fqbn flag or attach board to sketch") + os.Exit(errorcodes.ErrGeneric) + } - if _, err := upload.Upload(context.Background(), &rpc.UploadReq{ + if _, err = upload.Upload(context.Background(), &rpc.UploadReq{ Instance: instance, Fqbn: fqbn, SketchPath: sketchPath.String(),