github.com/tcnksm/gcli@v0.2.4-0.20170129033839-7eb950507e5a/README.md (about)

     1  gcli
     2  ====
     3  
     4  [![GitHub release](http://img.shields.io/github/release/tcnksm/gcli.svg?style=flat-square)][release]
     5   [![Travis](https://img.shields.io/travis/tcnksm/gcli.svg?style=flat-square)][travis]
     6  [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license]
     7  [![Go Documentation](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][godocs]
     8  
     9  [release]: https://github.com/tcnksm/gcli/releases
    10  [travis]: https://travis-ci.org/tcnksm/gcli
    11  [license]: https://github.com/tcnksm/gcli/blob/master/LICENSE
    12  [godocs]: http://godoc.org/github.com/tcnksm/gcli
    13  
    14  `gcli` generates a skeleton (codes and its directory structure) you need to start building Command Line Interface (CLI) tool by Golang right out of the box. You can use your favorite [CLI framework](#frameworks).
    15  
    16  ## Why ?
    17  
    18  Why you need `gcli`? Because you should focus on writing core function of CLI, not on interface. During developing CLI tool by Golang, you may find you're writing the chunk of [boilerplate code](https://en.wikipedia.org/wiki/Boilerplate_code) for interfaces. Stop writing the same codes every time. `gcli` generates them and save you a large amount of time by writing such code. This is like [Rails scaffold](http://guides.rubyonrails.org/command_line.html#rails-generate). Not only that, `gcli` know the best practices of golang CLI framework library which you want to use. Generated codes follows the most ideal way of using that framework, and you don't need to know about that. See the [frameworks](#frameworks) it supports now. 
    19  
    20  ## Demo
    21  
    22  The following demo shows creating `todo` CLI application which has `add`, `list` and `delete` command with [mitchellh/cli](https://github.com/mitchellh/cli) (Which is used for [Hashicorp](https://hashicorp.com/) products) with **one command**. As you can see, the generated codes are `go build`-able from beginning. 
    23  
    24  ![gif](/doc/gif/gcli-new.gif)
    25  
    26  And this [video](https://vimeo.com/142134929) shows creating same `todo` CLI application with `design` & `apply` commands. This is the other way to start building new CLI application. First, it starts with creating design file by `design` command. In this file, you can define, CLI name, description of the CLI , framework you want to use, and commands & flags with its usages. After editing, it executes `apply` command to generating a project from that design file. 
    27  
    28  ## Usage
    29  
    30  `gcli` is single command-line application. This application then takes subcommands. To check the all available commands,
    31  
    32  ```bash
    33  $ gcli help
    34  ```
    35  
    36  To get help for any specific subcommand, run it with the `-h` flag.
    37  
    38  `gcli` has 2 main subcommand to generate the project. The one is the `new` command, the other is the `design` & `apply` commands. The former is for generating the project by command line one-liner, the latter is for when you want to design it in your editor before generating (It generates design file and you can generate project based on it). The following section explain, how to use these commands.
    39  
    40  ### `new` command
    41  
    42  The `new` command tells gcli to generate CLI project with command-line one-liner,
    43  
    44  ```bash
    45  $ gcli new [options] NAME
    46  ```
    47  
    48  You must provides project name (`NAME`), the name will be the directory name it includes all codes and be the default binary name. By default, `gcli` creates a project under `$GOPATH/github.com/<username>` (If you don't provide username via option, it uses `github.user` or `user.name` in `.gitconfig` file). In option, you can set subcommand or flag it has and its description. You can also set your favorite [CLI framework](#frameworks) there. The followings are all available opntions,
    49  
    50  ```bash
    51  -command=name, -c           Command name which you want to add.
    52                              This is valid only when cli pacakge support commands.
    53                              This can be specified multiple times. Synopsis can be
    54                              set after ":". Namely, you can specify command by
    55                              -command=NAME:SYNOPSYS. Only NAME is required.
    56                              You can set multiple variables at same time with ","
    57                              separator.
    58  
    59  -flag=name, -f              Global flag option name which you want to add.
    60                              This can be specified multiple times. By default, flag type
    61                              is string and its description is empty. You can set them,
    62                              with ":" separator. Namaly, you can specify flag by
    63                              -flag=NAME:TYPE:DESCIRPTION. Order must be flow  this and
    64                              TYPE must be string, bool or int. Only NAME is required.
    65                              You can set multiple variables at same time with ","
    66                              separator.
    67  
    68  -framework=name, -F         Cli framework name. By default, gcli use "codegangsta/cli"
    69                              To check cli framework you can use, run 'gcli list'.
    70                              If you set invalid framework, it will be failed.
    71  
    72  -owner=name, -o             Command owner (author) name. This value is also used for
    73                              import path name. By default, owner name is extracted from
    74                              ~/.gitconfig variable.
    75  
    76  -skip-test, -T              Skip generating *_test.go file. By default, gcli generates
    77                              test file If you specify this flag, gcli will not generate
    78                              test files.
    79  ```
    80  
    81  For example, to `todo` CLI application which has `add`, `list` and `delete` command with [mitchellh/cli](https://github.com/mitchellh/cli),
    82  
    83  ```bash
    84  $ gcli new -F mitchellh_cli -c add -c list -c delete todo
    85  ```
    86  
    87  ### `design` & `apply` command
    88  
    89  The `design` command tells gcli to prepare design template file ([`.toml`](https://github.com/toml-lang/toml)). The design file defines all necessary information to generate CLI application. Some fields are filled with the ideal default value, and some have empty value. You can fill that empty filed with your favorite editor with thinking like what interface that should have or description of that and so on. You can see sample template file [`sample.toml`](/sample.toml). 
    90  
    91  After design, use `apply` command and tells gcli to generate CLI project based on the design file. The following describes this workflow. 
    92  
    93  First, generate design template file by `design` command, 
    94  
    95  ```bash
    96  $ gcli design [options] NAME
    97  ```
    98  You must provides project name (`NAME`). In option, you can set subcommand or flag it has and its description. You can also set your favorite [CLI framework](#frameworks) there. You can edit these values in design file later. 
    99  
   100  Then, edit design file by your favorite `$EDITOR`.
   101  
   102  ```bash
   103  $ $EDITOR <NAME>-design.toml
   104  ```
   105  
   106  After that validate design by `validate` command to check required fields are filled, 
   107  
   108  ```bash
   109  $ gcli validate <NAME>-design.toml
   110  ```
   111  
   112  Finnaly, generate CLI project with that design file by `apply` command, 
   113  
   114  ```bash
   115  $ gcli apply <NAME>-desigon.toml
   116  ```
   117  
   118  The video for this workflow is available on [Vimeo](https://vimeo.com/142134929). 
   119  
   120  ## Frameworks
   121  
   122  There are many framework (package) for buidling command line application by golang. For example, one of the most famous frameworks is [codegangsta/cli](https://github.com/codegangsta/cli). The framework helps you not writing many codes. But still you need to write many boilerplate code for that framework. And there are different way to use that framework and learning the ideal way to use is waste of time. gcli writes out with following the best practice for that framework (learn from famous tool that is built with that framework). 
   123  
   124  `gcli` can generate 2 types of CLI pattern. The one is [*sub-command pattern*](#sub-command), the other is [*flag pattern*](#flag). The former is flexible and you can add many behavior in one command application. The later is for simple application. You can check the all available frameworks by `list` command,
   125  
   126  ```bash
   127  $ gcli list
   128  ```
   129  
   130  To change framework, you can use `-framework` or `-F` option with the framework name. This option can be used for `new`, `design` and `apply` command. By default, [codegangsta_cli](https://github.com/codegangsta/cli) will be used. 
   131  
   132  The following section will explain [*sub-command pattern*](#sub-command) and [*flag pattern*](#flag). 
   133  
   134  ### Sub-Command
   135  
   136  *Sub-Command pattern* is the pattern that executable takes sub-command for change its behavior. `git` command is one example for this pattern. It takes `push`, `pull` subcommands. `gcli` is also this pattern. `gcli` supports the following frameworks for the command pattern.
   137  
   138  |Name|Sample projects|
   139  |:-:|:-:| 
   140  |[codegangsta_cli](https://github.com/codegangsta/cli) | [docker machine](https://github.com/docker/machine) | 
   141  |[mitchellh_cli](https://github.com/mitchellh/cli)| [consul](https://github.com/hashicorp/consul), [terraform](https://github.com/hashicorp/terraform)| 
   142  |[go_cmd](https://github.com/golang/go/blob/master/src/cmd/go/main.go#L30#L51)| [go](https://golang.org/cmd/go/)| 
   143  
   144  ([go_cmd](https://github.com/golang/go/blob/master/src/cmd/go/main.go#L30#L51) is not framework. It only uses standard package. It generates same struct and functions that `go` command uses.)
   145  
   146  ### Flag
   147  
   148  *Flag pattern* is the pattern that executable has flag options for changing its behavior. For example, `grep` command is this pattern. Now `gcli` only supports the official [flag](https://golang.org/pkg/flag/) package for this pattern.
   149  
   150  For example, to create command it has `-ignore-case` option and `context` option (your own `grep`),
   151  
   152  ```bash
   153  $ gcli new -F flag -flag=i:Bool -flag=C:Int grep
   154  ```
   155  
   156  ## Installation
   157  
   158  To install, use `go get` and `make install`. We tag versions so feel free to checkout that tag and compile.
   159  
   160  ```bash
   161  $ go get -d github.com/tcnksm/gcli
   162  $ cd $GOPATH/src/github.com/tcnksm/gcli
   163  $ make install 
   164  ```
   165  
   166  ## Contribution
   167  
   168  1. Fork ([https://github.com/tcnksm/gcli/fork](https://github.com/tcnksm/gcli/fork))
   169  1. Create a feature branch
   170  1. Commit your changes
   171  1. Rebase your local changes against the master branch
   172  1. Run test suite with the `make test` command and confirm that it passes
   173  1. Create a new Pull Request
   174  
   175  ## Author
   176  
   177  [Taichi Nakashima](https://github.com/tcnksm)