mkideal / cli

CLI - A package for building command line app with go
MIT License
730 stars 43 forks source link

Need to deal with the escape sequences #9

Closed suntong closed 8 years ago

suntong commented 8 years ago

Hi 王仕晋,

For the following,

Desc: "Golang package manager",
Text: `  gogo is a new golang package manager\n  very very good`,

I'll get

Golang package manager

  gogo is a new golang package manager\n  very very good

The github.com/voxelbrain/goptions was able to handle the escaping nicely, so should CLI. After all, the escaping sequence is a fundamental feature of modern programming language.

I'll change my sample code at https://github.com/suntong/lang/blob/master/lang/Go/src/sys/CommandLineGoptions.go to show that goptions handles the escaping nicely.

Done,

    Help      goptions.Help `goptions:"-h, --help, description='Show this help\n\nSub-commands (Verbs):\n\n\texecute\t\tExecute it\n\t\t\tExecute the given command\n\n\tdelete\t\tDelete it'"`
$ go run CommandLineGoptions.go
Usage: CommandLineGoptions [global options] <verb> [verb options]

Global options:
        -s, --server   Server to connect to
        -p, --password Don't prompt for password
        -t, --timeout  Connection timeout in seconds (default: 10s)
        -v, --verbose  Be verbose
        -q, --quiet    Do not print anything, even errors (except if --verbose is specified)
        -h, --help     Show this help

Sub-commands (Verbs):

    execute     Execute it
                Execute the given command

    delete     Delete it

Verbs:
    delete:
           -n, --name    Name of the entity to be deleted (*)
           -f, --force   Force removal
    execute:
           -c, --command Command to exectute (*)
               --script  Script to exectute

Please enable escape sequence handling.

Thanks

mkideal commented 8 years ago

You can replace ` with " like this:

Desc: "Golang package manager",
Text: "  gogo is a new golang package manager\n  very very good",

It's equivalent to:

Desc: "Golang package manager",
Text: `  gogo is a new golang package manager
  very very good`,

` is used to represent raw string in golang.

In fact, there is not any handle for escaping:

    Help      goptions.Help `goptions:"-h, --help, description='Show this help\n\nSub-commands (Verbs):\n\n\texecute\t\tExecute it\n\t\t\tExecute the given command\n\n\tdelete\t\tDelete it'"`

Try this:

// main.go
package main

import (
    "github.com/mkideal/cli"
)

type argT struct {
    cli.Helper
    Hello string `cli:"hello" usage:"no escaping\nhere is a new line"`
}

func main() {
    cli.Run(new(argT), func(ctx *cli.Context) error {
        return nil
    })
}
go run main.go -h
suntong commented 8 years ago

ops, yes. Silly me. :(