This commit is contained in:
Sangeeth Sudheer 2024-05-04 20:57:54 +05:30
parent 369fda6adb
commit 8143c2d08a
Signed by: x
GPG Key ID: F6D06ECE734C57D1
3 changed files with 115 additions and 42 deletions

31
cli/cli.go Normal file
View File

@ -0,0 +1,31 @@
package cli
import (
"flag"
"fmt"
"os"
)
func Cli() {
Args()
Flags()
}
func Args() {
fmt.Println("Program: ", os.Args[0])
fmt.Println("Args:", os.Args[1:])
}
func Flags() {
// NOTE: The flags must come before positional args or else these will take their default values
bookTypePtr := flag.String("booktype", "manga", "The type of book. Valid values are \"manga\", \"comic\", \"fiction\", \"non-fiction\".")
pagesPtr := flag.Uint64("pages", 0, "Number of pages in the book.")
var coverPath string
flag.StringVar(&coverPath, "cover", "", "Path to the cover image of the book.")
flag.Parse()
fmt.Println("Book type:", *bookTypePtr)
fmt.Println("# of Pages:", *pagesPtr)
fmt.Println("Cover path:", coverPath)
}

90
main.go
View File

@ -1,46 +1,50 @@
package main package main
// import "git.sangeeth.dev/gobyexample/runes" import (
// import "git.sangeeth.dev/gobyexample/structs" // "git.sangeeth.dev/gobyexample/runes"
// import "git.sangeeth.dev/gobyexample/generics" // "git.sangeeth.dev/gobyexample/structs"
// import "git.sangeeth.dev/gobyexample/errors" // "git.sangeeth.dev/gobyexample/generics"
// import "git.sangeeth.dev/gobyexample/goroutines" // "git.sangeeth.dev/gobyexample/errors"
// import "git.sangeeth.dev/gobyexample/channels" // "git.sangeeth.dev/gobyexample/goroutines"
// import "git.sangeeth.dev/gobyexample/cselect" // "git.sangeeth.dev/gobyexample/channels"
// import "git.sangeeth.dev/gobyexample/timeouts" // "git.sangeeth.dev/gobyexample/cselect"
// import "git.sangeeth.dev/gobyexample/selectdefault" // "git.sangeeth.dev/gobyexample/timeouts"
// import "git.sangeeth.dev/gobyexample/closingchannels" // "git.sangeeth.dev/gobyexample/selectdefault"
// import "git.sangeeth.dev/gobyexample/timers" // "git.sangeeth.dev/gobyexample/closingchannels"
// import "git.sangeeth.dev/gobyexample/tickers" // "git.sangeeth.dev/gobyexample/timers"
// import "git.sangeeth.dev/gobyexample/workerpools" // "git.sangeeth.dev/gobyexample/tickers"
// import "git.sangeeth.dev/gobyexample/waitgroups" // "git.sangeeth.dev/gobyexample/workerpools"
// import "git.sangeeth.dev/gobyexample/ratelimiting" // "git.sangeeth.dev/gobyexample/waitgroups"
// import "git.sangeeth.dev/gobyexample/atomics" // "git.sangeeth.dev/gobyexample/ratelimiting"
// import "git.sangeeth.dev/gobyexample/mutex" // "git.sangeeth.dev/gobyexample/atomics"
// import "git.sangeeth.dev/gobyexample/statefulgoroutines" // "git.sangeeth.dev/gobyexample/mutex"
// import "git.sangeeth.dev/gobyexample/sorting" // "git.sangeeth.dev/gobyexample/statefulgoroutines"
// import "git.sangeeth.dev/gobyexample/panic" // "git.sangeeth.dev/gobyexample/sorting"
// import "git.sangeeth.dev/gobyexample/defers" // "git.sangeeth.dev/gobyexample/panic"
// import "git.sangeeth.dev/gobyexample/recovering" // "git.sangeeth.dev/gobyexample/defers"
// import "git.sangeeth.dev/gobyexample/stringfuncs" // "git.sangeeth.dev/gobyexample/recovering"
// import "git.sangeeth.dev/gobyexample/formatting" // "git.sangeeth.dev/gobyexample/stringfuncs"
// import "git.sangeeth.dev/gobyexample/templates" // "git.sangeeth.dev/gobyexample/formatting"
// import "git.sangeeth.dev/gobyexample/regex" // "git.sangeeth.dev/gobyexample/templates"
// import "git.sangeeth.dev/gobyexample/json" // "git.sangeeth.dev/gobyexample/regex"
// import "git.sangeeth.dev/gobyexample/xml" // "git.sangeeth.dev/gobyexample/json"
// import "git.sangeeth.dev/gobyexample/time" // "git.sangeeth.dev/gobyexample/xml"
// import "git.sangeeth.dev/gobyexample/rand" // "git.sangeeth.dev/gobyexample/time"
// import "git.sangeeth.dev/gobyexample/numberparsing" // "git.sangeeth.dev/gobyexample/rand"
// import "git.sangeeth.dev/gobyexample/url" // "git.sangeeth.dev/gobyexample/numberparsing"
// import "git.sangeeth.dev/gobyexample/sha" // "git.sangeeth.dev/gobyexample/url"
// import "git.sangeeth.dev/gobyexample/base64" // "git.sangeeth.dev/gobyexample/sha"
// import "git.sangeeth.dev/gobyexample/files" // "git.sangeeth.dev/gobyexample/base64"
// import "git.sangeeth.dev/gobyexample/linefilter" // "git.sangeeth.dev/gobyexample/files"
// import "git.sangeeth.dev/gobyexample/exit" // "git.sangeeth.dev/gobyexample/linefilter"
// import "git.sangeeth.dev/gobyexample/filepath" // "git.sangeeth.dev/gobyexample/exit"
// import "git.sangeeth.dev/gobyexample/dir" // "git.sangeeth.dev/gobyexample/filepath"
// import "git.sangeeth.dev/gobyexample/tmp" // "git.sangeeth.dev/gobyexample/dir"
import "git.sangeeth.dev/gobyexample/embed" // "git.sangeeth.dev/gobyexample/tmp"
// "git.sangeeth.dev/gobyexample/embed"
// "git.sangeeth.dev/gobyexample/cli"
"git.sangeeth.dev/gobyexample/subcmd"
)
func main() { func main() {
// runes.Runes() // runes.Runes()
@ -83,5 +87,7 @@ func main() {
// filepath.Filepath() // filepath.Filepath()
// dir.Dir() // dir.Dir()
// tmp.Tmp() // tmp.Tmp()
embed.Embed() // embed.Embed()
// cli.Cli()
subcmd.SubCmd()
} }

36
subcmd/subcmd.go Normal file
View File

@ -0,0 +1,36 @@
package subcmd
import (
"flag"
"fmt"
"os"
)
func SubCmd() {
agentCmd := flag.NewFlagSet("agent", flag.ExitOnError)
agentLevel := agentCmd.Uint64("level", 1, "Level of the agent from 1-5")
agentName := agentCmd.String("name", "", "Name of the agent")
missionCmd := flag.NewFlagSet("mission", flag.ExitOnError)
missionName := missionCmd.String("codename", "classified", "Codename of the mission")
missionImpossible := missionCmd.Bool("impossible", false, "Is the mission impossible?")
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "A subcommand must be provided ('mission', 'agent')")
os.Exit(2)
}
switch os.Args[1] {
case "agent":
agentCmd.Parse(os.Args[2:])
fmt.Println("Agent level:", *agentLevel)
fmt.Println("Agent name:", *agentName)
case "mission":
missionCmd.Parse(os.Args[2:])
fmt.Println("Mission codename:", *missionName)
fmt.Println("Mission impossible?", *missionImpossible)
default:
fmt.Fprintln(os.Stderr, "Invalid subcommand. Only valid subcommands are 'mission' and 'agent'")
os.Exit(2)
}
}