Skip to content

Commit

Permalink
- [!] fix broken build
Browse files Browse the repository at this point in the history
  • Loading branch information
suntong committed Jan 19, 2022
1 parent 0fa4d4a commit 6b3a473
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 49 deletions.
10 changes: 8 additions & 2 deletions cc2py_cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Authors: Tong Sun

PackageName: main

Name: cc2pyC
Name: cc2py
Desc: Chinese-Character to Pinyin converter
Text: Converter Chinese to pinyin in different ways

Options:
- Name: Filei
Type: string
Flag: "i,in"
Usage: 'the Chinese text file to read from (or stdin)\n\t\t\tif not specified, read from command line instead'
Usage: 'the Chinese text file to read from (or "-" for stdin)'

- Name: Tone
Type: int
Expand Down Expand Up @@ -45,3 +45,9 @@ Options:
EnvV: true
Usage: capitalized each pinyin word

- Name: Args
Args: |
// positional arguments
// Args struct {
// CCStrs []string
// } `positional-args:"yes" required:"yes"`
25 changes: 11 additions & 14 deletions cc2py_cliDef.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
////////////////////////////////////////////////////////////////////////////
// Program: cc2pyC
// Program: cc2py
// Purpose: Chinese-Character to Pinyin converter
// Authors: Tong Sun (c) 2022, All rights reserved
////////////////////////////////////////////////////////////////////////////
Expand All @@ -18,12 +18,18 @@ import (

// The OptsT type defines all the configurable options from cli.
// type OptsT struct {
// Filei string `short:"i" long:"in" description:"the Chinese text file to read from (or stdin)\n\t\t\tif not specified, read from command line instead"`
// Filei string `short:"i" long:"in" description:"the Chinese text file to read from (or "-" for stdin)"`
// Tone int `short:"t" long:"tone" env:"CC2PY_TONE" description:"tone selection\n\t\t\t 0: normal. mnemonic~ nothing\n\t\t\t 1: tone at the end. mnemonic~ single sided\n\t\t\t 2: tone after yunmu. mnemonic~ double sided\n\t\t\t 3: tone on yunmu. mnemonic~ fancy"`
// Truncate int `short:"l" long:"truncate" env:"CC2PY_TRUNCATE" description:"select only part of the pinyin\n\t\t\t 0: normal. mnemonic~ nothing truncated\n\t\t\t 1: leave first char. mnemonic~ one\n\t\t\t 2: leave first shengmu. mnemonic~ might be two\n\t\t\t 9: leave only yunmu. mnemonic~ last"`
// Separator string `short:"s" long:"separator" env:"CC2PY_SEPARATOR" description:"separator string between each pinyin" default:" "`
// Polyphone bool `short:"p" long:"polyphone" env:"CC2PY_POLYPHONE" description:"polyphone support, output each polyphone pinyin available"`
// Capital bool `short:"c" long:"capitalized" env:"CC2PY_CAPITAL" description:"capitalized each pinyin word"`
//
// // positional arguments
// Args struct {
// CCStrs []string
// } `positional-args:"yes" required:"yes"`

// }

// Template for main starts here
Expand All @@ -32,7 +38,7 @@ import (
// Global variables definitions

// var (
// progname = "cc2pyC"
// progname = "cc2py"
// version = "0.1.0"
// date = "2022-01-17"

Expand All @@ -49,17 +55,8 @@ import (
// func main() {
//
// if _, err := parser.Parse(); err != nil {
// switch flagsErr := err.(type) {
// case flags.ErrorType:
// if flagsErr == flags.ErrHelp {
// os.Exit(0)
// }
// os.Exit(1)
// default:
// fmt.Println()
// parser.WriteHelp(os.Stdout)
// os.Exit(1)
// }
// parser.WriteHelp(os.Stdout)
// os.Exit(1)
// }
// fmt.Println("")
// }
Expand Down
87 changes: 54 additions & 33 deletions cc2py_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
package main

////////////////////////////////////////////////////////////////////////////
// Porgram: pinyin
// Purpose: pinyin conversion Go library
// Authors: Tong Sun (c) 2017, All rights reserved
// Credits:
// Program: cc2py
// Purpose: Chinese-Character to Pinyin converter
// Authors: Tong Sun (c) 2017-2022, All rights reserved
////////////////////////////////////////////////////////////////////////////

//go:generate sh -v cc2pyCLIGen.sh
//go:generate sh -v cc2py_cliGen.sh

import (
"fmt"
Expand All @@ -17,72 +16,94 @@ import (
"strings"

pinyin "github.com/go-cc/cc-pinyin"
"github.com/mkideal/cli"
"github.com/go-easygen/go-flags"
)

////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions

// The OptsT type defines all the configurable options from cli.
type OptsT struct {
Filei string `short:"i" long:"in" description:"the Chinese text file to read from (or \"-\" for stdin)"`
Tone int `short:"t" long:"tone" env:"CC2PY_TONE" description:"tone selection\n\t\t\t 0: normal. mnemonic~ nothing\n\t\t\t 1: tone at the end. mnemonic~ single sided\n\t\t\t 2: tone after yunmu. mnemonic~ double sided\n\t\t\t 3: tone on yunmu. mnemonic~ fancy"`
Truncate int `short:"l" long:"truncate" env:"CC2PY_TRUNCATE" description:"select only part of the pinyin\n\t\t\t 0: normal. mnemonic~ nothing truncated\n\t\t\t 1: leave first char. mnemonic~ one\n\t\t\t 2: leave first shengmu. mnemonic~ might be two\n\t\t\t 9: leave only yunmu. mnemonic~ last"`
Separator string `short:"s" long:"separator" env:"CC2PY_SEPARATOR" description:"separator string between each pinyin" default:" "`
Polyphone bool `short:"p" long:"polyphone" env:"CC2PY_POLYPHONE" description:"polyphone support, output each polyphone pinyin available"`
Capital bool `short:"c" long:"capitalized" env:"CC2PY_CAPITAL" description:"capitalized each pinyin word"`

// positional arguments
Args struct {
CCStrs []string
} `positional-args:"yes" required:"yes"`
}

////////////////////////////////////////////////////////////////////////////
// Global variables definitions

var (
progname = "cc2py"
VERSION = "0.2.2"
buildTime = "2021-12-18"
version = "0.2.2"
date = "2022-01-18"

// Opts store all the configurable options
Opts OptsT
)

var parser = flags.NewParser(&Opts, flags.Default)

////////////////////////////////////////////////////////////////////////////
// Function definitions

// Function main
func main() {
// default writer is os.Stdout
if err := cli.Root(root).Run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
if _, err := parser.Parse(); err != nil {
parser.WriteHelp(os.Stdout)
os.Exit(1)
}
fmt.Println("")
err := Validate()
abortOn("cli options", err)
cc2pyC()
}

// Validate implements cli.Validator interface
func (argv *rootT) Validate(ctx *cli.Context) error {
if argv.Tone < pinyin.Normal || argv.Tone > pinyin.Tone3 {
// Validate cli values sanity
func Validate() error {
if Opts.Tone < pinyin.Normal || Opts.Tone > pinyin.Tone3 {
return fmt.Errorf("tone %d out of range. run `%s -h` to get help.",
argv.Tone, progname)
Opts.Tone, progname)
}
if argv.Truncate < pinyin.Normal ||
argv.Truncate > pinyin.Initials && argv.Truncate < pinyin.ZeroConsonant ||
argv.Truncate > pinyin.Finals {
if Opts.Truncate < pinyin.Normal ||
Opts.Truncate > pinyin.Initials && Opts.Truncate < pinyin.ZeroConsonant ||
Opts.Truncate > pinyin.Finals {
return fmt.Errorf("truncate %d out of range. run `%s -h` to get help.",
argv.Truncate, progname)
Opts.Truncate, progname)
}
return nil
}

func cc2pyC(ctx *cli.Context) error {
// ctx.JSON(ctx.RootArgv())
// ctx.JSON(ctx.Argv())
// fmt.Println()
// fmt.Println(ctx.Args())
argv := ctx.Argv().(*rootT)

func cc2pyC() error {
// input data
var dd string
if ctx.IsSet("--in") { // -i,--in option is specified
data, err := ioutil.ReadAll(argv.Filei)
if len(Opts.Filei) != 0 { // -i,--in option is specified
var data []byte
var err error
if Opts.Filei == "-" {
data, err = ioutil.ReadAll(os.Stdin)
} else {
data, err = ioutil.ReadFile(Opts.Filei)
}
abortOn("Input", err)
argv.Filei.Close()
dd = string(data)
} else {
dd = strings.Join(ctx.Args(), " ")
dd = strings.Join(Opts.Args.CCStrs, " ")
if dd == "" {
ctx.WriteUsage()
parser.WriteHelp(os.Stdout)
os.Exit(0)
}
}

fmt.Println(cc2py(dd, argv.Tone, argv.Truncate,
argv.Separator, argv.Polyphone, argv.Capital))
fmt.Println(cc2py(dd, Opts.Tone, Opts.Truncate,
Opts.Separator, Opts.Polyphone, Opts.Capital))

return nil
}
Expand Down

0 comments on commit 6b3a473

Please sign in to comment.