wrap command line docs

This commit is contained in:
Arpad Ryszka 2018-01-09 01:34:26 +01:00
parent 395a6693d8
commit 8e0611f0b1
9 changed files with 658 additions and 624 deletions

View File

@ -14,7 +14,7 @@ var checkFailureTests = []mainTest{
"-syntax-string", "-syntax-string",
"-input", "-input",
"-input-string", "-input-string",
joinLines(positionalInputUsage), wrapLines(positionalInputUsage),
}, },
}, },
@ -30,7 +30,7 @@ var checkFailureTests = []mainTest{
"-syntax-string", "-syntax-string",
"-input", "-input",
"-input-string", "-input-string",
joinLines(positionalInputUsage), wrapLines(positionalInputUsage),
}, },
}, },
@ -46,7 +46,7 @@ var checkFailureTests = []mainTest{
"-syntax-string", "-syntax-string",
"-input", "-input",
"-input-string", "-input-string",
joinLines(positionalInputUsage), wrapLines(positionalInputUsage),
}, },
}, },
@ -62,7 +62,7 @@ var checkFailureTests = []mainTest{
"-syntax-string", "-syntax-string",
"-input", "-input",
"-input-string", "-input-string",
joinLines(positionalInputUsage), wrapLines(positionalInputUsage),
}, },
}, },
@ -78,7 +78,7 @@ var checkFailureTests = []mainTest{
"-syntax-string", "-syntax-string",
"-input", "-input",
"-input-string", "-input-string",
joinLines(positionalInputUsage), wrapLines(positionalInputUsage),
}, },
}, },
@ -94,7 +94,7 @@ var checkFailureTests = []mainTest{
"-syntax-string", "-syntax-string",
"-input", "-input",
"-input-string", "-input-string",
joinLines(positionalInputUsage), wrapLines(positionalInputUsage),
}, },
}, },
@ -110,7 +110,7 @@ var checkFailureTests = []mainTest{
"-syntax-string", "-syntax-string",
"-input", "-input",
"-input-string", "-input-string",
joinLines(positionalInputUsage), wrapLines(positionalInputUsage),
}, },
}, },
@ -212,14 +212,14 @@ func TestCheck(t *testing.T) {
"treerack", "check", "-help", "treerack", "check", "-help",
}, },
stdout: []string{ stdout: []string{
joinLines(checkUsage), wrapLines(checkUsage),
"-syntax", "-syntax",
"-syntax-string", "-syntax-string",
"-input", "-input",
"-input-string", "-input-string",
joinLines(positionalInputUsage), wrapLines(positionalInputUsage),
joinLines(checkExample), wrapLines(checkExample),
joinLines(docRef), wrapLines(docRef),
}, },
}) })

View File

@ -12,7 +12,7 @@ var checkSyntaxFailureTests = []mainTest{
stderr: []string{ stderr: []string{
"-syntax", "-syntax",
"-syntax-string", "-syntax-string",
joinLines(positionalSyntaxUsage), wrapLines(positionalSyntaxUsage),
}, },
}, },
@ -26,7 +26,7 @@ var checkSyntaxFailureTests = []mainTest{
"only one syntax", "only one syntax",
"-syntax", "-syntax",
"-syntax-string", "-syntax-string",
joinLines(positionalSyntaxUsage), wrapLines(positionalSyntaxUsage),
}, },
}, },
@ -40,7 +40,7 @@ var checkSyntaxFailureTests = []mainTest{
"only one syntax", "only one syntax",
"-syntax", "-syntax",
"-syntax-string", "-syntax-string",
joinLines(positionalSyntaxUsage), wrapLines(positionalSyntaxUsage),
}, },
}, },
@ -54,7 +54,7 @@ var checkSyntaxFailureTests = []mainTest{
"only one syntax", "only one syntax",
"-syntax", "-syntax",
"-syntax-string", "-syntax-string",
joinLines(positionalSyntaxUsage), wrapLines(positionalSyntaxUsage),
}, },
}, },
@ -68,7 +68,7 @@ var checkSyntaxFailureTests = []mainTest{
"missing syntax", "missing syntax",
"-syntax", "-syntax",
"-syntax-string", "-syntax-string",
joinLines(positionalSyntaxUsage), wrapLines(positionalSyntaxUsage),
}, },
}, },
@ -134,12 +134,12 @@ func TestCheckSyntax(t *testing.T) {
"treerack", "check-syntax", "-help", "treerack", "check-syntax", "-help",
}, },
stdout: []string{ stdout: []string{
joinLines(checkSyntaxUsage), wrapLines(checkSyntaxUsage),
"-syntax", "-syntax",
"-syntax-string", "-syntax-string",
joinLines(positionalSyntaxUsage), wrapLines(positionalSyntaxUsage),
joinLines(checkSyntaxExample), wrapLines(checkSyntaxExample),
joinLines(docRef), wrapLines(docRef),
}, },
}) })

View File

@ -1,12 +1,15 @@
package main package main
import "strings" import (
"strings"
"unicode/utf8"
)
const summary = `treerack - parser generator - https://github.com/aryszka/treerack` const summary = `treerack - parser generator - https://github.com/aryszka/treerack`
const commandsHelp = `Available commands: const commandsHelp = `Available commands:
check validates an arbitrary input against a syntax definition check validates arbitrary input against a syntax definition
parse parses an arbitrary input with a syntax definition into an abstract syntax tree parse parses arbitrary input with a syntax definition into an abstract syntax tree
check-syntax validates a syntax definition check-syntax validates a syntax definition
generate generates a parser from a syntax definition generate generates a parser from a syntax definition
help prints the current help help prints the current help
@ -40,8 +43,8 @@ const indentUsage = `string used for indentation of the printed AST`
const checkUsage = `'treerack check' takes a syntax description from a file or inline string, an arbitrary piece const checkUsage = `'treerack check' takes a syntax description from a file or inline string, an arbitrary piece
of text from the standard input, or a file, or inline string, and parses the input text with the defined syntax. of text from the standard input, or a file, or inline string, and parses the input text with the defined syntax.
It returns non-zero exit code and prints the problem if the provided syntax is not valid or the intput cannot be It returns non-zero exit code and prints the problem if the provided syntax is not valid or the input cannot be
parsed against it.` parsed with it.`
const checkExample = `Example: const checkExample = `Example:
treerack check -syntax example.treerack foo.example` treerack check -syntax example.treerack foo.example`
@ -67,6 +70,37 @@ standard output.`
const generateExample = `Example: const generateExample = `Example:
treerack generate example.treerack > parser.go` treerack generate example.treerack > parser.go`
func joinLines(s string) string { const wrap = 72
return strings.Replace(s, "\n", " ", -1)
func wrapLines(s string) string {
s = strings.Replace(s, "\n", " ", -1)
w := strings.Split(s, " ")
var l, ll []string
for i := 0; i < len(w); i++ {
ll = append(ll, w[i])
lineLength := utf8.RuneCount([]byte(strings.Join(ll, " ")))
if lineLength < wrap {
continue
}
if lineLength > wrap {
ll = ll[:len(ll)-1]
i--
}
if len(ll) == 0 {
l = append(l, w[i])
i++
} else {
l = append(l, strings.Join(ll, " "))
ll = nil
}
}
if len(ll) > 0 {
l = append(l, strings.Join(ll, " "))
}
return strings.Join(l, "\n")
} }

View File

@ -90,14 +90,14 @@ func TestGenerate(t *testing.T) {
"treerack", "generate", "-help", "treerack", "generate", "-help",
}, },
stdout: []string{ stdout: []string{
joinLines(generateUsage), wrapLines(generateUsage),
"-syntax", "-syntax",
"-syntax-string", "-syntax-string",
"-export", "-export",
"-package-name", "-package-name",
joinLines(positionalSyntaxUsage), wrapLines(positionalSyntaxUsage),
joinLines(generateExample), wrapLines(generateExample),
joinLines(docRef), wrapLines(docRef),
}, },
}) })

View File

@ -26,7 +26,7 @@ func (o *fileOptions) multipleInputsError() {
stderr("Options:") stderr("Options:")
o.flagSet.PrintDefaults() o.flagSet.PrintDefaults()
stderr() stderr()
stderr(joinLines(o.positionalDoc)) stderr(wrapLines(o.positionalDoc))
} }
func (o *fileOptions) missingInputError() { func (o *fileOptions) missingInputError() {
@ -35,7 +35,7 @@ func (o *fileOptions) missingInputError() {
stderr("Options:") stderr("Options:")
o.flagSet.PrintDefaults() o.flagSet.PrintDefaults()
stderr() stderr()
stderr(joinLines(o.positionalDoc)) stderr(wrapLines(o.positionalDoc))
} }
func (o *fileOptions) getSource() (hasInput bool, fileName string, inline string, code int) { func (o *fileOptions) getSource() (hasInput bool, fileName string, inline string, code int) {

View File

@ -13,9 +13,9 @@ type commandOptions struct {
func initOptions(usage, example, positionalDoc string, args []string) *commandOptions { func initOptions(usage, example, positionalDoc string, args []string) *commandOptions {
var o commandOptions var o commandOptions
o.usage = joinLines(usage) o.usage = wrapLines(usage)
o.example = joinLines(example) o.example = wrapLines(example)
o.positionalDoc = joinLines(positionalDoc) o.positionalDoc = wrapLines(positionalDoc)
o.args = args o.args = args
o.flagSet = flag.NewFlagSet("", flag.ContinueOnError) o.flagSet = flag.NewFlagSet("", flag.ContinueOnError)
@ -26,12 +26,12 @@ func initOptions(usage, example, positionalDoc string, args []string) *commandOp
} }
func (o *commandOptions) boolFlag(v *bool, name, usage string) { func (o *commandOptions) boolFlag(v *bool, name, usage string) {
usage = joinLines(usage) usage = wrapLines(usage)
o.flagSet.BoolVar(v, name, *v, usage) o.flagSet.BoolVar(v, name, *v, usage)
} }
func (o *commandOptions) stringFlag(v *string, name, usage string) { func (o *commandOptions) stringFlag(v *string, name, usage string) {
usage = joinLines(usage) usage = wrapLines(usage)
o.flagSet.StringVar(v, name, *v, usage) o.flagSet.StringVar(v, name, *v, usage)
} }
@ -65,7 +65,7 @@ func (o *commandOptions) printHelp() {
stdout() stdout()
stdout(o.example) stdout(o.example)
stdout() stdout()
stdout(joinLines(docRef)) stdout(wrapLines(docRef))
} }
func (o *commandOptions) help() bool { func (o *commandOptions) help() bool {

View File

@ -116,16 +116,16 @@ func TestParse(t *testing.T) {
"treerack", "parse", "-help", "treerack", "parse", "-help",
}, },
stdout: []string{ stdout: []string{
joinLines(parseUsage), wrapLines(parseUsage),
"-syntax", "-syntax",
"-syntax-string", "-syntax-string",
"-input", "-input",
"-input-string", "-input-string",
"-pretty", "-pretty",
"-indent", "-indent",
joinLines(positionalInputUsage), wrapLines(positionalInputUsage),
joinLines(parseExample), wrapLines(parseExample),
joinLines(docRef), wrapLines(docRef),
}, },
}) })

View File

@ -1,14 +1,14 @@
[next] [next]
no random generator output and generator output check no random generator output and generator output check
errors errors
spellcheck
linting
releasing
formatter formatter
report unused parsers report unused parsers
parse hashed, storing only the results parse hashed, storing only the results
linux packaging linux packaging
[cmd]
help for positional argument
[errors] [errors]
take the last take the last
test error report on invalid flag test error report on invalid flag

File diff suppressed because it is too large Load Diff