diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index 68c9a14d..44801f69 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -42,14 +42,21 @@ func evaluateAll(cmd *cobra.Command, args []string) error { colorsEnabled = true } - if writeInplace && len(args) < 2 { + firstFileIndex := -1 + if !nullInput && len(args) == 1 { + firstFileIndex = 0 + } else if len(args) > 1 { + firstFileIndex = 1 + } + + if writeInplace && (firstFileIndex == -1) { return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file") } if writeInplace { // only use colors if its forced colorsEnabled = forceColor - writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[1]) + writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[firstFileIndex]) out, err = writeInPlaceHandler.CreateTempFile() if err != nil { return err diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index 54ce2eb1..7f17df26 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -62,14 +62,21 @@ func evaluateSequence(cmd *cobra.Command, args []string) error { colorsEnabled = true } - if writeInplace && len(args) < 2 { + firstFileIndex := -1 + if !nullInput && len(args) == 1 { + firstFileIndex = 0 + } else if len(args) > 1 { + firstFileIndex = 1 + } + + if writeInplace && (firstFileIndex == -1) { return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file") } if writeInplace { // only use colors if its forced colorsEnabled = forceColor - writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[1]) + writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[firstFileIndex]) out, err = writeInPlaceHandler.CreateTempFile() if err != nil { return err