Skip to content

Commit 63e449f

Browse files
authored
Refactor cli (#52)
* docs: Update readme, add demo * refactor: cmd to make it more posix * chore(deps): Add missed tool
1 parent 1f33a16 commit 63e449f

File tree

11 files changed

+1054
-298
lines changed

11 files changed

+1054
-298
lines changed

.github/images/demo_1.png

-24.7 KB
Binary file not shown.

.github/images/demo_2.png

-40.5 KB
Binary file not shown.

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ This repository contains solutions for puzzles and cli tool to run solutions to
9494
<summary>2017</summary>
9595

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

224224
Run it and follow instructions
225225

226+
Cli support optional metrics, to enable them you can use following flags:
227+
228+
```text
229+
--elapsed, -e Enables elapsed time metric
230+
--bench, -b Enables benchmark metric
231+
```
232+
233+
All available flags:
234+
235+
```text
236+
COMMANDS:
237+
help, h Shows a list of commands or help for one command
238+
239+
GLOBAL OPTIONS:
240+
--elapsed, -e Enables elapsed time metric (default: false)
241+
--bench, -b Enables benchmark metric (default: false)
242+
--help, -h show help (default: false)
243+
--version, -v print the version (default: false)
244+
```
245+
246+
226247
### Demo
227248

228-
![cli demo_step_1](.github/images/demo_1.png)
229-
![cli demo_step_2](.github/images/demo_2.png)
249+
[![asciicast](https://asciinema.org/a/9UFklCUVZTQHCRsHD2vybTlMb.svg)](https://asciinema.org/a/9UFklCUVZTQHCRsHD2vybTlMb)

cmd/aoc-cli/commands.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/urfave/cli/v2"
7+
)
8+
9+
func commands(ctx context.Context) []*cli.Command {
10+
const (
11+
cmdRun = "run"
12+
)
13+
14+
cmds := []*cli.Command{
15+
{
16+
Name: cmdRun,
17+
Aliases: nil,
18+
Usage: "Runs advent-of-code application",
19+
UsageText: "",
20+
Description: "",
21+
ArgsUsage: "",
22+
Category: "",
23+
BashComplete: nil,
24+
Before: nil,
25+
After: nil,
26+
Action: menu(ctx),
27+
OnUsageError: nil,
28+
Subcommands: nil,
29+
Flags: cmdRunFlags(),
30+
SkipFlagParsing: false,
31+
HideHelp: false,
32+
HideHelpCommand: false,
33+
Hidden: false,
34+
UseShortOptionHandling: false,
35+
HelpName: "",
36+
CustomHelpTemplate: "",
37+
},
38+
}
39+
40+
return cmds
41+
}

cmd/aoc-cli/flags.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"github.com/urfave/cli/v2"
5+
)
6+
7+
const (
8+
flagElapsed = "elapsed"
9+
flagShortElapsed = "e"
10+
flagBenchmark = "bench"
11+
flagShortBenchmark = "b"
12+
)
13+
14+
func cmdRunFlags() []cli.Flag {
15+
var res []cli.Flag
16+
17+
elapsed := cli.BoolFlag{
18+
Name: flagElapsed,
19+
Aliases: []string{flagShortElapsed},
20+
Usage: "Enables elapsed time metric",
21+
EnvVars: nil,
22+
FilePath: "",
23+
Required: false,
24+
Hidden: false,
25+
Value: false,
26+
DefaultText: "",
27+
Destination: nil,
28+
HasBeenSet: false,
29+
}
30+
31+
benchmark := cli.BoolFlag{
32+
Name: flagBenchmark,
33+
Aliases: []string{flagShortBenchmark},
34+
Usage: "Enables benchmark metric",
35+
EnvVars: nil,
36+
FilePath: "",
37+
Required: false,
38+
Hidden: false,
39+
Value: false,
40+
DefaultText: "",
41+
Destination: nil,
42+
HasBeenSet: false,
43+
}
44+
45+
res = append(res, &elapsed, &benchmark)
46+
47+
return res
48+
}

0 commit comments

Comments
 (0)