-
Notifications
You must be signed in to change notification settings - Fork 1
/
case_converter_cli.go
60 lines (49 loc) · 1.33 KB
/
case_converter_cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"log"
"github.com/jessevdk/go-flags"
)
var opts struct {
From string `short:"f" long:"from" required:"true" name:"Format to convert from"`
To string `short:"t" long:"to" required:"true" name:"Format to convert to"`
}
const help string = `
Usage:
$ case_converter -f format_from -t format_to some_text
Examples:
$ case_converter --from camel --to snake someText
-> some_text
$ case_converter -f pascal -t kebab SomeText
-> some-text
Options:
-f, --from format of the input
-t, --to format of the output
Format options (accepted by the -f and -t flags):
* snake (e.g. some_example_sentence)
* camel (e.g. someExampleSentence)
* pascal (e.g. SomeExampleSentence)
* lower (e.g. some example sentence)
* upper (e.g. SOME EXAMPLE SENTENCE)
* title (e.g. Some Example Sentence)
* sentence (e.g. Some example sentence)
* flat (e.g. someexamplesentence) (not supported for -t flag)
* kebab (e.g. some-example-sentence)
* dot (e.g. some.example.sentence)
`
func main() {
args, err := flags.Parse(&opts)
if err != nil {
fmt.Println(help)
log.Fatal(err)
}
if len(args) != 1 {
fmt.Println(help)
log.Fatal("case_converter accepts a single argument")
}
converted, err := ConvertCase(opts.From, opts.To, args[0])
if err != nil {
log.Fatal(err)
}
fmt.Println(converted)
}