Modernize usage of urfave/cli

This commit is contained in:
Noah 2020-06-04 23:11:03 -07:00
parent de896c93e6
commit eae7258ce1
7 changed files with 64 additions and 58 deletions

View File

@ -22,26 +22,28 @@ import (
// Convert between image files (png or bitmap) and Doodle drawing files (levels // Convert between image files (png or bitmap) and Doodle drawing files (levels
// and doodads) // and doodads)
var Convert cli.Command var Convert *cli.Command
func init() { func init() {
Convert = cli.Command{ Convert = &cli.Command{
Name: "convert", Name: "convert",
Usage: "convert between images and Doodle drawing files", Usage: "convert between images and Doodle drawing files",
ArgsUsage: "<input> <output>", ArgsUsage: "<input> <output>",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{ &cli.StringFlag{
Name: "key", Name: "key",
Usage: "chroma key color for transparency on input image files", Usage: "chroma key color for transparency on input image files",
Value: "#ffffff", Value: "#ffffff",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "title, t", Name: "title",
Usage: "set the title of the level or doodad being created", Aliases: []string{"t"},
Usage: "set the title of the level or doodad being created",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "palette, p", Name: "palette",
Usage: "use a palette JSON to define color swatch properties", Aliases: []string{"p"},
Usage: "use a palette JSON to define color swatch properties",
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
@ -63,7 +65,7 @@ func init() {
) )
} }
args := c.Args() args := c.Args().Slice()
var ( var (
inputFiles = args[:len(args)-1] inputFiles = args[:len(args)-1]
inputType = strings.ToLower(filepath.Ext(inputFiles[0])) inputType = strings.ToLower(filepath.Ext(inputFiles[0]))

View File

@ -11,43 +11,43 @@ import (
) )
// EditDoodad allows writing doodad metadata. // EditDoodad allows writing doodad metadata.
var EditDoodad cli.Command var EditDoodad *cli.Command
func init() { func init() {
EditDoodad = cli.Command{ EditDoodad = &cli.Command{
Name: "edit-doodad", Name: "edit-doodad",
Usage: "update metadata for a Doodad file", Usage: "update metadata for a Doodad file",
ArgsUsage: "<filename.doodad>", ArgsUsage: "<filename.doodad>",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ &cli.BoolFlag{
Name: "quiet, q", Name: "quiet, q",
Usage: "limit output (don't show doodad data at the end)", Usage: "limit output (don't show doodad data at the end)",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "title", Name: "title",
Usage: "set the doodad title", Usage: "set the doodad title",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "author", Name: "author",
Usage: "set the doodad author", Usage: "set the doodad author",
}, },
cli.StringSliceFlag{ &cli.StringSliceFlag{
Name: "tag, t", Name: "tag, t",
Usage: "set a key/value tag on the doodad, in key=value format. Empty value deletes the tag.", Usage: "set a key/value tag on the doodad, in key=value format. Empty value deletes the tag.",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "hide", Name: "hide",
Usage: "Hide the doodad from the palette", Usage: "Hide the doodad from the palette",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "unhide", Name: "unhide",
Usage: "Unhide the doodad from the palette", Usage: "Unhide the doodad from the palette",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "lock", Name: "lock",
Usage: "write-lock the level file", Usage: "write-lock the level file",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "unlock", Name: "unlock",
Usage: "remove the write-lock on the level file", Usage: "remove the write-lock on the level file",
}, },
@ -60,7 +60,7 @@ func init() {
) )
} }
var filenames = c.Args() var filenames = c.Args().Slice()
for _, filename := range filenames { for _, filename := range filenames {
if err := editDoodad(c, filename); err != nil { if err := editDoodad(c, filename); err != nil {
log.Error("%s: %s", filename, err) log.Error("%s: %s", filename, err)

View File

@ -10,47 +10,48 @@ import (
) )
// EditLevel allows writing level metadata. // EditLevel allows writing level metadata.
var EditLevel cli.Command var EditLevel *cli.Command
func init() { func init() {
EditLevel = cli.Command{ EditLevel = &cli.Command{
Name: "edit-level", Name: "edit-level",
Usage: "update metadata for a Level file", Usage: "update metadata for a Level file",
ArgsUsage: "<filename.level>", ArgsUsage: "<filename.level>",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ &cli.BoolFlag{
Name: "quiet, q", Name: "quiet",
Usage: "limit output (don't show doodad data at the end)", Aliases: []string{"q"},
Usage: "limit output (don't show doodad data at the end)",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "title", Name: "title",
Usage: "set the level title", Usage: "set the level title",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "author", Name: "author",
Usage: "set the level author", Usage: "set the level author",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "password", Name: "password",
Usage: "set the level password", Usage: "set the level password",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "type", Name: "type",
Usage: "set the page type. One of: Unbounded, Bounded, NoNegativeSpace, Bordered", Usage: "set the page type. One of: Unbounded, Bounded, NoNegativeSpace, Bordered",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "max-size", Name: "max-size",
Usage: "set the page max size (WxH format, like 2550x3300)", Usage: "set the page max size (WxH format, like 2550x3300)",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "wallpaper", Name: "wallpaper",
Usage: "set the wallpaper filename", Usage: "set the wallpaper filename",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "lock", Name: "lock",
Usage: "write-lock the level file", Usage: "write-lock the level file",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "unlock", Name: "unlock",
Usage: "remove the write-lock on the level file", Usage: "remove the write-lock on the level file",
}, },
@ -63,7 +64,7 @@ func init() {
) )
} }
var filenames = c.Args() var filenames = c.Args().Slice()
for _, filename := range filenames { for _, filename := range filenames {
if err := editLevel(c, filename); err != nil { if err := editLevel(c, filename); err != nil {
log.Error("%s: %s", filename, err) log.Error("%s: %s", filename, err)

View File

@ -10,15 +10,15 @@ import (
) )
// InstallScript to add the script to a doodad file. // InstallScript to add the script to a doodad file.
var InstallScript cli.Command var InstallScript *cli.Command
func init() { func init() {
InstallScript = cli.Command{ InstallScript = &cli.Command{
Name: "install-script", Name: "install-script",
Usage: "install the JavaScript source to a doodad", Usage: "install the JavaScript source to a doodad",
ArgsUsage: "<index.js> <filename.doodad>", ArgsUsage: "<index.js> <filename.doodad>",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{ &cli.StringFlag{
Name: "key", Name: "key",
Usage: "chroma key color for transparency on input image files", Usage: "chroma key color for transparency on input image files",
Value: "#ffffff", Value: "#ffffff",
@ -34,8 +34,8 @@ func init() {
var ( var (
args = c.Args() args = c.Args()
scriptFile = args[0] scriptFile = args.Get(0)
doodadFile = args[1] doodadFile = args.Get(1)
) )
// Read the JavaScript source. // Read the JavaScript source.

View File

@ -13,29 +13,30 @@ import (
) )
// Show information about a Level or Doodad file. // Show information about a Level or Doodad file.
var Show cli.Command var Show *cli.Command
func init() { func init() {
Show = cli.Command{ Show = &cli.Command{
Name: "show", Name: "show",
Usage: "show information about a level or doodad file", Usage: "show information about a level or doodad file",
ArgsUsage: "<.level or .doodad>", ArgsUsage: "<.level or .doodad>",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ &cli.BoolFlag{
Name: "actors", Name: "actors",
Usage: "print verbose actor data in Level files", Usage: "print verbose actor data in Level files",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "chunks", Name: "chunks",
Usage: "print verbose data about all the pixel chunks in a file", Usage: "print verbose data about all the pixel chunks in a file",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "script", Name: "script",
Usage: "print the script from a doodad file and exit", Usage: "print the script from a doodad file and exit",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "verbose, v", Name: "verbose",
Usage: "print verbose output (all verbose flags enabled)", Aliases: []string{"v"},
Usage: "print verbose output (all verbose flags enabled)",
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
@ -46,7 +47,7 @@ func init() {
) )
} }
filenames := c.Args() filenames := c.Args().Slice()
for _, filename := range filenames { for _, filename := range filenames {
switch strings.ToLower(filepath.Ext(filename)) { switch strings.ToLower(filepath.Ext(filename)) {
case enum.LevelExt: case enum.LevelExt:

View File

@ -44,13 +44,13 @@ func main() {
) )
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.BoolFlag{ &cli.BoolFlag{
Name: "debug, d", Name: "debug, d",
Usage: "enable debug level logging", Usage: "enable debug level logging",
}, },
} }
app.Commands = []cli.Command{ app.Commands = []*cli.Command{
commands.Convert, commands.Convert,
commands.Show, commands.Show,
commands.EditLevel, commands.EditLevel,

View File

@ -55,15 +55,17 @@ func main() {
) )
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.BoolFlag{ &cli.BoolFlag{
Name: "debug, d", Name: "debug",
Usage: "enable debug level logging", Aliases: []string{"d"},
Usage: "enable debug level logging",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "edit, e", Name: "edit",
Usage: "edit the map given on the command line (instead of play it)", Aliases: []string{"e"},
Usage: "edit the map given on the command line (instead of play it)",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "guitest", Name: "guitest",
Usage: "enter the GUI Test scene on startup", Usage: "enter the GUI Test scene on startup",
}, },