github.com/kamiazya/dot-github@v1.3.0/cli.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  )
     9  
    10  type Parsed struct {
    11  	Help             bool
    12  	Version          bool
    13  	IssueOnly        bool
    14  	PROnly           bool
    15  	ContributingOnly bool
    16  	SelfUpdate       bool
    17  	flags            *flag.FlagSet
    18  }
    19  
    20  const Usage = `Usage: dot-github [flags]
    21  
    22    A CLI tool to generate GitHub files such as CONTRIBUTING.md,
    23    ISSUE_TEMPLATE.md and PULL_REQUEST_TEMPLATE.md from template files in
    24    '~/.github' directory.
    25  
    26    You can control which template should be used and it attempts to generate
    27    all by default.
    28  
    29    Below templates are looked by dot-github command.
    30    ($DOT_GITHUB_HOME is defaulted to $HOME)
    31  
    32      $DOT_GITHUB_HOME/.github/ISSUE_TEMPLATE.md
    33      $DOT_GITHUB_HOME/.github/PULL_REQUEST_TEMPLATE.md
    34      $DOT_GITHUB_HOME/.github/CONTRIBUTING.md
    35  
    36    You can use Golang's standard template for the template files.
    37    Below variables are available by default.
    38  
    39      .IsIssue        : (boolean) true when used for issue template
    40      .IsPullRequest  : (boolean) true when used for pull request template
    41      .IsContributing : (boolean) true when used for contributing template
    42      .RepoName       : (string) repository name
    43      .RepoUser       : (string) repository owner name
    44  
    45  References:
    46  
    47    GitHub Blog:     https://github.com/blog/2111-issue-and-pull-request-templates
    48    More usage:      https://github.com/rhysd/dot-github#readme
    49    Golang template: https://golang.org/pkg/text/template/
    50  
    51  Flags:`
    52  
    53  func (p *Parsed) ShowUsage(out io.Writer) {
    54  	fmt.Fprintln(out, Usage)
    55  	p.flags.SetOutput(out)
    56  	p.flags.PrintDefaults()
    57  }
    58  
    59  func ParseCmdArgs(err_out io.Writer) (*Parsed, error) {
    60  	var (
    61  		help         bool
    62  		version      bool
    63  		issue        bool
    64  		pr           bool
    65  		contributing bool
    66  		selfUpdate   bool
    67  	)
    68  
    69  	flags := flag.NewFlagSet("dot-github", flag.ContinueOnError)
    70  	flags.SetOutput(err_out)
    71  
    72  	flags.BoolVar(&help, "help", false, "Show this help")
    73  	flags.BoolVar(&version, "version", false, "Show version")
    74  	flags.BoolVar(&issue, "issue", false, "Import ISSUE_TEMPLATE.md only")
    75  	flags.BoolVar(&pr, "pullrequest", false, "Import PULL_REQUEST_TEMPLATE.md only")
    76  	flags.BoolVar(&contributing, "contributing", false, "Import CONTRIBUTING.md only")
    77  	flags.BoolVar(&selfUpdate, "selfupdate", false, "Update dot-github binary itself")
    78  
    79  	flags.Usage = func() {
    80  		fmt.Fprintln(err_out, Usage)
    81  		flags.PrintDefaults()
    82  	}
    83  
    84  	if err := flags.Parse(os.Args[1:]); err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	return &Parsed{
    89  		help,
    90  		version,
    91  		issue,
    92  		pr,
    93  		contributing,
    94  		selfUpdate,
    95  		flags,
    96  	}, nil
    97  }