Skip to content

Commit 10daaa8

Browse files
committed
- [*] add sub-commands to the program
1 parent e947e18 commit 10daaa8

5 files changed

Lines changed: 140 additions & 37 deletions

File tree

cmdArch.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Program: picv
3+
// Purpose: picture vault
4+
// Authors: Tong Sun (c) 2017, All rights reserved
5+
////////////////////////////////////////////////////////////////////////////
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
12+
"github.com/mkideal/cli"
13+
)
14+
15+
////////////////////////////////////////////////////////////////////////////
16+
// Constant and data type/structure definitions
17+
18+
////////////////////////////////////////////////////////////////////////////
19+
// arch
20+
21+
func archCLI(ctx *cli.Context) error {
22+
rootArgv := ctx.RootArgv().(*rootT)
23+
argv := ctx.Argv().(*archT)
24+
fmt.Printf("[arch]:\n %+v\n %+v\n %v\n", rootArgv, argv, ctx.Args())
25+
return nil
26+
}

cmdCut.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ type OptsT struct {
2323

2424
var Opts OptsT
2525

26-
//==========================================================================
27-
// Main dispatcher
26+
////////////////////////////////////////////////////////////////////////////
27+
// cut
2828

29-
func picv(ctx *cli.Context) error {
30-
ctx.JSON(ctx.RootArgv())
31-
fmt.Println()
32-
ctx.JSON(ctx.Argv())
33-
fmt.Println()
34-
argv := ctx.Argv().(*rootT)
35-
Opts.Gap, Opts.Pod, Opts.Verbose = argv.Gap, argv.Pod, argv.Verbose.Value()
29+
func cutCLI(ctx *cli.Context) error {
30+
rootArgv := ctx.RootArgv().(*rootT)
31+
argv := ctx.Argv().(*cutT)
32+
Opts.Gap, Opts.Pod, Opts.Verbose = argv.Gap, argv.Pod, rootArgv.Verbose.Value()
3633
ctx.JSON(Opts)
3734
fmt.Println()
3835

36+
return picVault()
37+
}
38+
39+
func picVault() error {
3940
return nil
4041
}

picv.yaml

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ PackageName: main
66
Name: picv
77
Desc: "picture vault"
88
Text: Tool to deal with camera pictures and put them in vault
9-
#NumArg
9+
Global: true
1010
#NumOption: cli.AtLeast(1)
11-
# Handle the rest of CLI args ourselves
12-
CanSubRoute: true
11+
NumArg: cli.AtLeast(1)
1312

14-
UsageLead: "Usage:\\n picv [Options] dir [dirs...]"
13+
#UsageLead: "Usage:\\n picv [Options] dir [dirs...]"
1514

1615
Options:
1716
# - Name: Dir
@@ -20,19 +19,37 @@ Options:
2019
# Usage: pic source dir
2120
# Value: "./"
2221

23-
- Name: Gap
24-
Type: int
25-
Flag: g,gap
26-
Usage: Gap in days to be considered as different group/vault
27-
Value: 5
28-
29-
- Name: Pod
30-
Type: int
31-
Flag: p,pod
32-
Usage: Minimum number of picture to have before splitting to a different group/vault
33-
Value: 15
34-
3522
- Name: Verbose
3623
Type: cli.Counter
3724
Flag: v,verbose
3825
Usage: Verbose mode (Multiple -v options increase the verbosity.)
26+
27+
Command:
28+
29+
- Name: cut
30+
Desc: "Separate picture into groups"
31+
# NumArg: cli.AtLeast(1)
32+
#NumOption: cli.AtLeast(1)
33+
# Handle the rest of CLI args ourselves
34+
CanSubRoute: true
35+
36+
Options:
37+
- Name: Gap
38+
Type: int
39+
Flag: g,gap
40+
Usage: Gap in days to be considered as different group/vault
41+
Value: 5
42+
43+
- Name: Pod
44+
Type: int
45+
Flag: p,pod
46+
Usage: Minimum number of picture to have before splitting to a different group/vault
47+
Value: 15
48+
49+
- Name: arch
50+
Desc: "Archive groups of picture into vaults"
51+
# NumArg: cli.AtLeast(1)
52+
#NumOption: cli.AtLeast(1)
53+
# Handle the rest of CLI args ourselves
54+
CanSubRoute: true
55+

picvCLIDef.go

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ import (
1818

1919
type rootT struct {
2020
cli.Helper
21-
Gap int `cli:"g,gap" usage:"Gap in days to be considered as different group/vault" dft:"5"`
22-
Pod int `cli:"p,pod" usage:"Minimum number of picture to have before splitting to a different group/vault" dft:"15"`
2321
Verbose cli.Counter `cli:"v,verbose" usage:"Verbose mode (Multiple -v options increase the verbosity.)"`
2422
}
2523

2624
var root = &cli.Command{
27-
Name: "picv",
28-
Desc: "picture vault\nbuilt on " + buildTime,
29-
Text: "Tool to deal with camera pictures and put them in vault" +
30-
"\n\nUsage:\n picv [Options] dir [dirs...]",
31-
Argv: func() interface{} { return new(rootT) },
32-
Fn: picv,
25+
Name: "picv",
26+
Desc: "picture vault\nbuilt on " + buildTime,
27+
Text: "Tool to deal with camera pictures and put them in vault",
28+
Global: true,
29+
Argv: func() interface{} { return new(rootT) },
30+
Fn: picv,
3331

34-
CanSubRoute: true,
32+
NumArg: cli.AtLeast(1),
3533
}
3634

3735
// Template for main starts here
@@ -52,7 +50,9 @@ var root = &cli.Command{
5250
// cli.SetUsageStyle(cli.ManualStyle) // up-down, for left-right, use NormalStyle
5351
// //NOTE: You can set any writer implements io.Writer
5452
// // default writer is os.Stdout
55-
// if err := cli.Root(root,).Run(os.Args[1:]); err != nil {
53+
// if err := cli.Root(root,
54+
// cli.Tree(cutDef),
55+
// cli.Tree(archDef)).Run(os.Args[1:]); err != nil {
5656
// fmt.Fprintln(os.Stderr, err)
5757
// }
5858
// fmt.Println("")
@@ -71,3 +71,49 @@ var root = &cli.Command{
7171
// }
7272

7373
// Template for CLI handling starts here
74+
75+
////////////////////////////////////////////////////////////////////////////
76+
// cut
77+
78+
// func cutCLI(ctx *cli.Context) error {
79+
// rootArgv = ctx.RootArgv().(*rootT)
80+
// argv := ctx.Argv().(*cutT)
81+
// fmt.Printf("[cut]:\n %+v\n %+v\n %v\n", rootArgv, argv, ctx.Args())
82+
// return nil
83+
// }
84+
85+
type cutT struct {
86+
Gap int `cli:"g,gap" usage:"Gap in days to be considered as different group/vault" dft:"5"`
87+
Pod int `cli:"p,pod" usage:"Minimum number of picture to have before splitting to a different group/vault" dft:"15"`
88+
}
89+
90+
var cutDef = &cli.Command{
91+
Name: "cut",
92+
Desc: "Separate picture into groups",
93+
Argv: func() interface{} { return new(cutT) },
94+
Fn: cutCLI,
95+
96+
CanSubRoute: true,
97+
}
98+
99+
////////////////////////////////////////////////////////////////////////////
100+
// arch
101+
102+
// func archCLI(ctx *cli.Context) error {
103+
// rootArgv = ctx.RootArgv().(*rootT)
104+
// argv := ctx.Argv().(*archT)
105+
// fmt.Printf("[arch]:\n %+v\n %+v\n %v\n", rootArgv, argv, ctx.Args())
106+
// return nil
107+
// }
108+
109+
type archT struct {
110+
}
111+
112+
var archDef = &cli.Command{
113+
Name: "arch",
114+
Desc: "Archive groups of picture into vaults",
115+
Argv: func() interface{} { return new(archT) },
116+
Fn: archCLI,
117+
118+
CanSubRoute: true,
119+
}

picvMain.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
var (
2222
progname = "picv"
23-
VERSION = "0.1.1"
23+
VERSION = "0.2.0"
2424
buildTime = "2017-06-03"
2525
)
2626

@@ -31,8 +31,21 @@ var (
3131
func main() {
3232
//NOTE: You can set any writer implements io.Writer
3333
// default writer is os.Stdout
34-
if err := cli.Root(root).Run(os.Args[1:]); err != nil {
34+
if err := cli.Root(root,
35+
cli.Tree(cutDef),
36+
cli.Tree(archDef)).Run(os.Args[1:]); err != nil {
3537
fmt.Fprintln(os.Stderr, err)
3638
}
3739
fmt.Println("")
3840
}
41+
42+
//==========================================================================
43+
// Main dispatcher
44+
45+
func picv(ctx *cli.Context) error {
46+
ctx.JSON(ctx.RootArgv())
47+
ctx.JSON(ctx.Argv())
48+
fmt.Println()
49+
50+
return nil
51+
}

0 commit comments

Comments
 (0)