Last active
October 29, 2016 00:29
-
-
Save suntong/cb2bf3d3ada7d7941aac8ef338a815a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- go -*- | |
//////////////////////////////////////////////////////////////////////////// | |
// Program: dump | |
// Purpose: dump assistant program | |
//////////////////////////////////////////////////////////////////////////// | |
package main | |
import ( | |
"fmt" | |
"github.com/mkideal/cli" | |
clix "github.com/mkideal/cli/ext" | |
"os" | |
) | |
//////////////////////////////////////////////////////////////////////////// | |
// dump | |
type rootT struct { | |
cli.Helper | |
} | |
var root = &cli.Command{ | |
Name: "dump", | |
Desc: "dump assistant program", | |
Text: "Swiss-army assistant toolkit", | |
Argv: func() interface{} { return new(rootT) }, | |
Fn: dump, | |
NumArg: cli.ExactN(1), | |
} | |
func main() { | |
cli.SetUsageStyle(cli.ManualStyle) // up-down, for left-right, use NormalStyle | |
//NOTE: You can set any writer implements io.Writer | |
// default writer is os.Stdout | |
if err := cli.Root(root, | |
cli.Tree(dumpDef)).Run(os.Args[1:]); err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
} | |
fmt.Println("") | |
} | |
func dump(ctx *cli.Context) error { | |
ctx.JSON(ctx.RootArgv()) | |
ctx.JSON(ctx.Argv()) | |
fmt.Println() | |
return nil | |
} | |
//////////////////////////////////////////////////////////////////////////// | |
// dump | |
type dumpT struct { | |
Filei *clix.Reader `cli:"*i,input" usage:"The file to dump from (mandatory)"` | |
Fileo *clix.Writer `cli:"o,output" usage:"The dump output (default: .dump file of input)"` | |
ID bool `cli:"id" usage:"Dump ID as well"` | |
} | |
var dumpDef = &cli.Command{ | |
Name: "dump", | |
Desc: "*Dump as text-only file", | |
Argv: func() interface{} { return new(dumpT) }, | |
Fn: dumpCLI, | |
NumArg: cli.AtLeast(1), | |
CanSubRoute: true, | |
} | |
func dumpCLI(ctx *cli.Context) error { | |
rootArgv := ctx.RootArgv().(*rootT) | |
argv := ctx.Argv().(*dumpT) | |
fmt.Printf("[dump]:\n %+v\n %+v\n %v\n", rootArgv, argv, ctx.Args()) | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# program name, name for the executable | |
ProgramName: dump | |
PackageName: main | |
Name: dump | |
Desc: "dump assistant program" | |
Text: Swiss-army assistant toolkit | |
NumArg: cli.ExactN(1) | |
Command: | |
- Name: dump | |
Desc: "*Dump as text-only file" | |
NumOption: cli.AtLeast(1) | |
Options: | |
- Name: Filei | |
Type: clix.Reader | |
Flag: '*i,input' | |
Usage: The file to dump from (mandatory) | |
- Name: Fileo | |
Type: clix.Writer | |
Flag: o,output | |
Usage: 'The dump output (default: .dump file of input)' | |
- Name: ID | |
Type: bool | |
Flag: id | |
Usage: Dump ID as well |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment