Skip to content

Commit

Permalink
Implement -l -list to list all commands. Fixed #2
Browse files Browse the repository at this point in the history
  • Loading branch information
dim0x69 committed Oct 23, 2024
1 parent ac905be commit 86bd68c
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ type CodeBlock struct {
Lang string // the infostring from the code fence
Code string // the content of the code fence
Meta map[string]any // contains metadata for the code block

}

// CommandBlock represents a heading, which contains one to multiple code fences.
Expand All @@ -89,21 +88,38 @@ type CommandBlock struct {
Meta map[string]any // placeholder for the future
}

func listCommands(commands map[string]CommandBlock) {
fmt.Println("Available commands:")
for name, command := range commands {
if len(command.Dependencies) > 0 {
fmt.Printf("%s: %s)\n", name, command.Dependencies)
} else {
fmt.Println(name)
}
}
}

func main() {
setLogLevel()
fileFlag := flag.String("file", "", "Specify a markdown file")
fileFlagShort := flag.String("f", "", "Specify a markdown file (shorthand)")
listFlag := flag.Bool("list", false, "list commands")
listFlagShort := flag.Bool("l", false, "list commands (shorthand)")
flag.Parse()

if *fileFlagShort != "" {
fileFlag = fileFlagShort
}

if *listFlagShort {
listFlag = listFlagShort
}

logrus.Debug("MDX started with parameters:", os.Args)

// Check for subcommands
if flag.NArg() < 1 {
errorExit("Usage: mdx [-file <markdown-file>] <command> [args]")
if flag.NArg() < 1 && !*listFlag {
errorExit("Usage: mdx [-file <markdown-file>] [-list] <command> [args]")
}

commandName := flag.Arg(0)
Expand All @@ -125,6 +141,11 @@ func main() {
}
}

if *listFlag {
listCommands(commands)
os.Exit(0)
}

if command, ok := commands[commandName]; ok {
err := executeCommandBlock(commands, &command, commandArgs...)
if err != nil {
Expand Down

0 comments on commit 86bd68c

Please sign in to comment.