Skip to content

Commit

Permalink
Accept +1234 argument in any place
Browse files Browse the repository at this point in the history
Fixes the issue reported with PR #158:
#158 (comment)
  • Loading branch information
walles committed Sep 29, 2023
1 parent 242c050 commit faafc49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
17 changes: 8 additions & 9 deletions moar.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ func tryOpen(filename string) error {
// line number, and returns the remaining args.
//
// Returns 0 on no target line number specified.
func getTargetLineNumberOneBased(flagSet *flag.FlagSet) (int, []string) {
args := flagSet.Args()
func getTargetLineNumberOneBased(args []string) (int, []string) {
for i, arg := range args {
if !strings.HasPrefix(arg, "+") {
continue
Expand Down Expand Up @@ -378,7 +377,9 @@ func main() {
flags = append(strings.Fields(moarEnv), flags...)
}

err := flagSet.Parse(flags)
targetLineNumberOneBased, remainingArgs := getTargetLineNumberOneBased(flags)

err := flagSet.Parse(remainingArgs)
if err != nil {
if err == flag.ErrHelp {
printUsage(os.Stdout, flagSet, false)
Expand Down Expand Up @@ -408,10 +409,8 @@ func main() {
TimestampFormat: time.StampMicro,
})

targetLineNumberOneBased, remainingArgs := getTargetLineNumberOneBased(flagSet)

if len(remainingArgs) > 1 {
fmt.Fprintln(os.Stderr, "ERROR: Expected exactly one filename, or data piped from stdin, got:", remainingArgs)
if len(flagSet.Args()) > 1 {
fmt.Fprintln(os.Stderr, "ERROR: Expected exactly one filename, or data piped from stdin, got:", flagSet.Args())
fmt.Fprintln(os.Stderr)
printUsage(os.Stderr, flagSet, true)

Expand All @@ -421,8 +420,8 @@ func main() {
stdinIsRedirected := !term.IsTerminal(int(os.Stdin.Fd()))
stdoutIsRedirected := !term.IsTerminal(int(os.Stdout.Fd()))
var inputFilename *string
if len(remainingArgs) == 1 {
word := remainingArgs[0]
if len(flagSet.Args()) == 1 {
word := flagSet.Args()[0]
inputFilename = &word

// Need to check before twin.NewScreen() below, otherwise the screen
Expand Down
7 changes: 7 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ if ./moar does-not-exist >&/dev/null; then
exit 1
fi

echo Testing not crashing with different argument orders...
./moar +123 moar.go >/dev/null
./moar moar.go +123 >/dev/null
./moar +123 --trace moar.go >/dev/null
./moar --trace +123 moar.go >/dev/null
./moar --trace moar.go +123 >/dev/null

echo Test --version...
./moar --version >/dev/null # Should exit with code 0
diff -u <(./moar --version) <(git describe --tags --dirty --always)
Expand Down

0 comments on commit faafc49

Please sign in to comment.