Skip to content

Commit

Permalink
feat: help command
Browse files Browse the repository at this point in the history
  • Loading branch information
fpfeng committed Jan 16, 2024
1 parent a2c6779 commit 5d8ab6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
28 changes: 24 additions & 4 deletions cli.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"fmt"
"os"
"slices"

log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

const Version = "0.0.3"
const DefaultPort = 5731

type CLIParseResult struct {
Expand All @@ -17,18 +19,32 @@ type CLIParseResult struct {
}

func StartCLI(cliArgs []string) *CLIParseResult {
cli.AppHelpTemplate = `
NAME:
{{.Name}} - {{.Usage}}
USAGE:
quickurl /path/to/file1 /path/to/file2
quickurl -s /path/to/file1 -s /path/to/file2 -p 8080
{{if .Commands}}
OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}`
result := CLIParseResult{Port: 0, Files: make([]string, 0)}
app := &cli.App{
Name: fmt.Sprintf("QuickURL %v", Version),
Usage: "Sharing file with http instantly",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "p",
Usage: "listening port",
Usage: "listening `port number`",
Value: DefaultPort,
Destination: &result.Port,
},
&cli.StringSliceFlag{
Name: "s",
Usage: "serving filepath",
Name: "s",
Usage: "serving filepath, -s /path/to/file1 -s /path/to/file2",
TakesFile: true,
},
&cli.BoolFlag{
Name: "v",
Expand All @@ -47,6 +63,7 @@ func StartCLI(cliArgs []string) *CLIParseResult {
}
log.Debugf("args: %v", rawArgs)

servingArgfiles := cCtx.StringSlice("s")
simpleModeFiles := make([]string, 0)
for _, arg := range rawArgs {
log.Debug(arg)
Expand All @@ -60,8 +77,11 @@ func StartCLI(cliArgs []string) *CLIParseResult {
}
if len(simpleModeFiles) > 0 {
result.Files = append(result.Files, simpleModeFiles...)
} else if len(servingArgfiles) > 0 {
result.Files = append(result.Files, servingArgfiles...)
} else {
result.Files = append(result.Files, cCtx.StringSlice("s")...)
cli.ShowAppHelp(cCtx)
return nil
}
return nil
},
Expand Down
3 changes: 3 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type QuickURL struct {
}

func NewQuickURL(cliConfig *CLIParseResult) *QuickURL {
if len(cliConfig.Files) == 0 {
os.Exit(0)
}
qu := &QuickURL{
ListeningPort: cliConfig.Port,
ServingFiles: map[string]string{},
Expand Down

0 comments on commit 5d8ab6b

Please sign in to comment.