Skip to content

Commit fc6aede

Browse files
committed
Improve argument passing
This commit makes argument passing more flexible by supporting single-dash, single-letter arguments as well as non-flag, positional arguments with no preceding dashes.
1 parent 41544cc commit fc6aede

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

quickserv.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"io"
1212
"io/fs"
13-
"io/ioutil"
1413
"log"
1514
"math/big"
1615
"net"
@@ -126,9 +125,10 @@ func GetLocalIP() string {
126125

127126
// DecodeForm performs URL query unescaping on encoded form data to make parsing
128127
// easier. Remaining encoded strings are:
129-
// % -> %25
130-
// & -> %26
131-
// = -> %3D
128+
//
129+
// % -> %25
130+
// & -> %26
131+
// = -> %3D
132132
//
133133
// If "%" is not encoded first in the pre-encoding step, then it will encode the
134134
// percent signs from the encoding of & and = in addition to real percent signs,
@@ -183,7 +183,7 @@ func IsWSL() bool {
183183
}
184184

185185
for _, filename := range filesToCheck {
186-
raw, err := ioutil.ReadFile(filename)
186+
raw, err := os.ReadFile(filename)
187187
if err == nil && r.Match(raw) {
188188
return true
189189
}
@@ -267,9 +267,17 @@ func GetShebang(path string) string {
267267
return strings.TrimSuffix(strings.TrimPrefix(string(result), "#!"), "\r")
268268
}
269269

270-
// GetFormAsArguments converts a parsed form such that the variable name=value
271-
// becomes the following. Names are always preceded by two dashes "--".
272-
// []string{"--name", "value"}
270+
// GetFormAsArguments converts a parsed form such that the variables:
271+
//
272+
// name=value
273+
// n=val
274+
// noval=
275+
//
276+
// become the following. Multi-character names are preceded by two dashes "--"
277+
// while single-character names are preceded by one dash "-". Names with no
278+
// value are passed literally with no preceding dashes.
279+
//
280+
// []string{"--name", "value", "-n", "val", "noval"}
273281
//
274282
// No guarantees are made about the order of the variables in the resulting
275283
// slice, except that every name directly precedes its respective value.
@@ -279,9 +287,14 @@ func GetFormAsArguments(form url.Values) []string {
279287
if k != "" {
280288
for _, v := range vs {
281289
if v != "" {
282-
result = append(result, "--"+k, v)
290+
switch len(k) {
291+
case 1:
292+
result = append(result, "-"+k, v)
293+
default:
294+
result = append(result, "--"+k, v)
295+
}
283296
} else {
284-
result = append(result, "--"+k)
297+
result = append(result, k)
285298
}
286299
}
287300
} else {
@@ -744,7 +757,7 @@ https://github.com/jstrieb/quickserv`)
744757
localIP := GetLocalIP()
745758
logger.Println("Starting a server...")
746759
fmt.Printf("Visit http://%v:%v to access the server from the local network.\n", localIP, port)
747-
fmt.Println("Press Control + C or close this window to stop the server.\n")
760+
fmt.Print("Press Control + C or close this window to stop the server.\n\n")
748761

749762
// Build a handler that decides whether to serve static files or dynamically
750763
// execute them

0 commit comments

Comments
 (0)