Skip to content

Commit 68303cc

Browse files
committed
feat: make 'get' the default command
Allows 'wikitool Earth' instead of 'wikitool get Earth'. Dynamically checks registered subcommands rather than hardcoded list.
1 parent b281be7 commit 68303cc

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

cmd/root.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,47 @@ var (
1212
)
1313

1414
var rootCmd = &cobra.Command{
15-
Use: "wikitool",
15+
Use: "wikitool [title|url]",
1616
Short: "Fetch Wikipedia content via the REST API",
1717
Long: `wikitool is a command-line tool for fetching Wikipedia content.
1818
1919
It supports all language Wikipedias and can accept either page titles
2020
or full Wikipedia URLs as input.
2121
22+
When called with a title or URL directly, it fetches the page content
23+
(equivalent to 'wikitool get').
24+
2225
Examples:
23-
wikitool get Earth
24-
wikitool get https://de.wikipedia.org/wiki/Erde
26+
wikitool Earth
27+
wikitool https://de.wikipedia.org/wiki/Erde
28+
wikitool -L "ISO 7064"
2529
wikitool search "solar system" --limit 10
2630
wikitool languages Earth`,
2731
}
2832

2933
func Execute() {
34+
// Default to "get" if first arg isn't a subcommand
35+
if len(os.Args) > 1 && !isSubcommand(os.Args[1]) {
36+
os.Args = append([]string{os.Args[0], "get"}, os.Args[1:]...)
37+
}
38+
3039
if err := rootCmd.Execute(); err != nil {
3140
os.Exit(1)
3241
}
3342
}
3443

44+
func isSubcommand(arg string) bool {
45+
if arg == "help" || arg == "-h" || arg == "--help" {
46+
return true
47+
}
48+
for _, cmd := range rootCmd.Commands() {
49+
if cmd.Name() == arg || cmd.HasAlias(arg) {
50+
return true
51+
}
52+
}
53+
return false
54+
}
55+
3556
func init() {
3657
rootCmd.PersistentFlags().StringVarP(&lang, "lang", "l", "en", "Wikipedia language code")
3758
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "text", "Output format: text, json, html")

0 commit comments

Comments
 (0)