Skip to content

Commit

Permalink
Fix crash if no flags were specified
Browse files Browse the repository at this point in the history
  • Loading branch information
flwyd committed Sep 18, 2024
1 parent 7204c40 commit 8e6ccd2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions adifmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func runMain(prepare func(l *adif.Logfile)) int {
// filenames can come before or after flags, but not interspersed
args := os.Args[2:]
firstflag := slices.IndexFunc(args, func(s string) bool { return strings.HasPrefix(s, "-") })
if firstflag < 0 {
firstflag = len(args)
}
nonflags := args[0:firstflag]
args = args[firstflag:]
fs.Parse(args)
Expand Down
55 changes: 55 additions & 0 deletions adifmt/testdata/cat_flag_order.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ exec adifmt cat --output json --json-indent 2 -- -with-hyphen.tsv bar.tsv
cmp stdout combined.json
! stderr .

# No flags
exec adifmt cat foo.tsv bar.tsv
cmp stdout combined.adi
! stderr .

# Single file, no flags
exec adifmt cat bar.tsv
cmp stdout bar.adi
! stderr .

# Pipe input, no flags
stdin bar.tsv
exec adifmt cat
cmp stdout bar.adi
! stderr .

# Pipe input, change format
stdin bar.tsv
exec adifmt cat --output adx
cmp stdout bar.adx
! stderr .

-- foo.tsv --
CALL MODE
K1A CW
Expand Down Expand Up @@ -63,3 +85,36 @@ W4D RTTY
}
]
}
-- combined.adi --
Generated with 4 records by https://github.com/flwyd/adif-multitool
<ADIF_VER:5>3.1.4 <CREATED_TIMESTAMP:15>23450607 080910 <PROGRAMID:6>adifmt <PROGRAMVERSION:7>(devel) <EOH>
<CALL:3>K1A <MODE:2>CW <EOR>
<CALL:3>W2B <MODE:3>SSB <EOR>
<CALL:3>K3C <MODE:2>FM <EOR>
<CALL:3>W4D <MODE:4>RTTY <EOR>
-- bar.adi --
Generated with 2 records by https://github.com/flwyd/adif-multitool
<ADIF_VER:5>3.1.4 <CREATED_TIMESTAMP:15>23450607 080910 <PROGRAMID:6>adifmt <PROGRAMVERSION:7>(devel) <EOH>
<CALL:3>K3C <MODE:2>FM <EOR>
<CALL:3>W4D <MODE:4>RTTY <EOR>
-- bar.adx --
<?xml version="1.0" encoding="UTF-8"?>
<ADX>
<HEADER>
<!--Generated with 2 records by https://github.com/flwyd/adif-multitool-->
<ADIF_VER>3.1.4</ADIF_VER>
<CREATED_TIMESTAMP>23450607 080910</CREATED_TIMESTAMP>
<PROGRAMID>adifmt</PROGRAMID>
<PROGRAMVERSION>(devel)</PROGRAMVERSION>
</HEADER>
<RECORDS>
<RECORD>
<CALL>K3C</CALL>
<MODE>FM</MODE>
</RECORD>
<RECORD>
<CALL>W4D</CALL>
<MODE>RTTY</MODE>
</RECORD>
</RECORDS>
</ADX>
11 changes: 11 additions & 0 deletions adifmt/testdata/version.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Tests the version command.

exec adifmt version
cmp stdout expected.txt
! stderr .

-- expected.txt --
adifmt version (devel)
Built from git revision (unknown)
ADIF version 3.1.4 from https://adif.org/314/ADIF_314.htm
See https://github.com/flwyd/adif-multitool for details

0 comments on commit 8e6ccd2

Please sign in to comment.