godocs

package module
v0.0.0-...-2d9428f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2016 License: MIT Imports: 7 Imported by: 13

README

godocs

This is the fork of docopt-go.

Usage:

func Parse(
	doc string, version string, settings ...interface{},
) (map[string]interface{}, error)
func MustParse(
    doc string, version string, settings ...interface{},
) map[string]interface{}

Settings:

Usage

Usage("blah") - use blah instead of parsing docs for usage section.

Options

Options("-test") - use -test instead of parsing docs for options section.

Args

Args([]string{"-x"}) - use -x instead of os.Args.

OptionsFirst

OptionsFirst - pass this setting if options should be passed as first arguments.

NoExit

NoExit - pass this setting if program shouldn't exits after help/version commands.

UsePager

UsePager - pass this setting if help should be displayed using pager (will be used $PAGER or less).

Example
Parse(
    `
example

Usage:
    example [commands] [options]

Commands:
    -C Create something.
    -R Remove something.

Options:
    -w Specify whatever.
    `,
    "1.0",
    UsePager,
    Usage(`
    example -C
    example -R -w
    example -h
`),
)

Documentation

Overview

Package godocs parses command-line arguments based on a help message.

⚠ Use the alias “godocs”:

import "github.com/kovetskiy/godocs"

or

$ go get github.com/kovetskiy/godocs

Index

Examples

Constants

View Source
const (
	// OptionsFirst pass this setting if options should be passed as first
	// args.
	OptionsFirst int = iota

	// NoExit can be used if program should not exit if help/version argument
	// passed.
	NoExit

	// UsePager instead of printing docs.
	UsePager
)

Variables

This section is empty.

Functions

func MustParse

func MustParse(
	doc string,
	version string,
	settings ...interface{},
) map[string]interface{}

func Parse

func Parse(
	doc string, version string, settings ...interface{},
) (map[string]interface{}, error)

Parse `argv` based on the command-line interface described in `doc`.

Given a conventional command-line help message, godocs creates a parser and processes the arguments. See https://github.com/kovetskiy/godocs#help-message-format for a description of the help message format. If `argv` is `nil`, `os.Args[1:]` is used.

godocs returns a map of option names to the values parsed from `argv`, and an error or `nil`.

Set `help` to `false` to disable automatic help messages on `-h` or `--help`. If `version` is a non-empty string, it will be printed when `--version` is specified. Set `optionsFirst` to `true` to require that options always come before positional arguments; otherwise they can overlap.

By default, godocs calls `os.Exit(0)` if it handled a built-in option such as `-h` or `--version`. If the user errored with a wrong command or options, godocs exits with a return code of 1. To stop godocs from calling `os.Exit()` and to handle your own return codes, pass an optional last parameter of `false` for `exit`.

Example
usage := `Usage:
  config_example tcp [<host>] [--force] [--timeout=<seconds>]
  config_example serial <port> [--baud=<rate>] [--timeout=<seconds>]
  config_example -h | --help | --version`
// parse the command line `comfig_example tcp 127.0.0.1 --force`
argv := []string{"tcp", "127.0.0.1", "--force"}
arguments, _ := Parse(usage, "1.0", Args(argv))
// sort the keys of the arguments map
var keys []string
for k := range arguments {
	keys = append(keys, k)
}
sort.Strings(keys)
// print the argument keys and values
for _, k := range keys {
	fmt.Printf("%9s %v\n", k, arguments[k])
}
Output:
   --baud <nil>
  --force true
   --help false
--timeout <nil>
--version false
       -h false
   <host> 127.0.0.1
   <port> <nil>
   serial false
      tcp true

Types

type Args

type Args []string

Args instead of os.Args

type LanguageError

type LanguageError struct {
	// contains filtered or unexported fields
}

LanguageError records an error with the doc string.

func (LanguageError) Error

func (e LanguageError) Error() string

type Options

type Options string

Options can be used for handling specific options, which should be used instead of parsing docs.

type Usage

type Usage string

Usage can be used for handling specific usage, which should be used by godocs instead of parsing docs.

type UserError

type UserError struct {
	Usage string
	// contains filtered or unexported fields
}

UserError records an error with program arguments.

func (UserError) Error

func (e UserError) Error() string

Directories

Path Synopsis
examples
arguments command
calculator command
config_file command
counted command
git command
naval_fate command
odd_even command
options command
quick command
type_assert command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL