Skip to content

Commit

Permalink
Refactor cli (#52)
Browse files Browse the repository at this point in the history
* docs: Update readme, add demo

* refactor: cmd to make it more posix

* chore(deps): Add missed tool
  • Loading branch information
obalunenko authored Nov 20, 2021
1 parent 1f33a16 commit 63e449f
Show file tree
Hide file tree
Showing 11 changed files with 1,054 additions and 298 deletions.
Binary file removed .github/images/demo_1.png
Binary file not shown.
Binary file removed .github/images/demo_2.png
Binary file not shown.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ This repository contains solutions for puzzles and cli tool to run solutions to
<summary>2017</summary>

- [x] [Day 1: Inverse Captcha](https://adventofcode.com/2017/day/1)
- [ ] [Day 2: Corruption Checksum](https://adventofcode.com/2017/day/2)
- [x] [Day 2: Corruption Checksum](https://adventofcode.com/2017/day/2)
- [ ] [Day 3: Spiral Memory](https://adventofcode.com/2017/day/3)
- [ ] [Day 4: High-Entropy Passphrases](https://adventofcode.com/2017/day/4)
- [ ] [Day 5: A Maze of Twisty Trampolines, All Alike](https://adventofcode.com/2017/day/5)
Expand Down Expand Up @@ -223,7 +223,27 @@ and execute

Run it and follow instructions

Cli support optional metrics, to enable them you can use following flags:

```text
--elapsed, -e Enables elapsed time metric
--bench, -b Enables benchmark metric
```

All available flags:

```text
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--elapsed, -e Enables elapsed time metric (default: false)
--bench, -b Enables benchmark metric (default: false)
--help, -h show help (default: false)
--version, -v print the version (default: false)
```


### Demo

![cli demo_step_1](.github/images/demo_1.png)
![cli demo_step_2](.github/images/demo_2.png)
[![asciicast](https://asciinema.org/a/9UFklCUVZTQHCRsHD2vybTlMb.svg)](https://asciinema.org/a/9UFklCUVZTQHCRsHD2vybTlMb)
41 changes: 41 additions & 0 deletions cmd/aoc-cli/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"

"github.com/urfave/cli/v2"
)

func commands(ctx context.Context) []*cli.Command {
const (
cmdRun = "run"
)

cmds := []*cli.Command{
{
Name: cmdRun,
Aliases: nil,
Usage: "Runs advent-of-code application",
UsageText: "",
Description: "",
ArgsUsage: "",
Category: "",
BashComplete: nil,
Before: nil,
After: nil,
Action: menu(ctx),
OnUsageError: nil,
Subcommands: nil,
Flags: cmdRunFlags(),
SkipFlagParsing: false,
HideHelp: false,
HideHelpCommand: false,
Hidden: false,
UseShortOptionHandling: false,
HelpName: "",
CustomHelpTemplate: "",
},
}

return cmds
}
48 changes: 48 additions & 0 deletions cmd/aoc-cli/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"github.com/urfave/cli/v2"
)

const (
flagElapsed = "elapsed"
flagShortElapsed = "e"
flagBenchmark = "bench"
flagShortBenchmark = "b"
)

func cmdRunFlags() []cli.Flag {
var res []cli.Flag

elapsed := cli.BoolFlag{
Name: flagElapsed,
Aliases: []string{flagShortElapsed},
Usage: "Enables elapsed time metric",
EnvVars: nil,
FilePath: "",
Required: false,
Hidden: false,
Value: false,
DefaultText: "",
Destination: nil,
HasBeenSet: false,
}

benchmark := cli.BoolFlag{
Name: flagBenchmark,
Aliases: []string{flagShortBenchmark},
Usage: "Enables benchmark metric",
EnvVars: nil,
FilePath: "",
Required: false,
Hidden: false,
Value: false,
DefaultText: "",
Destination: nil,
HasBeenSet: false,
}

res = append(res, &elapsed, &benchmark)

return res
}
Loading

0 comments on commit 63e449f

Please sign in to comment.