Show help as the default command

pull/6/head
Mickaël Menu 3 years ago
parent 7e3e2cbd20
commit aac1c17a13
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -15,7 +15,7 @@ import (
type Edit struct {
Filtering
Sorting
Force bool `help:"Don't confirm before editing many notes at the same time" short:"f"`
Force bool `help:"Don't confirm before editing many notes at the same time." short:"f"`
}
func (cmd *Edit) Run(container *Container) error {

@ -13,29 +13,29 @@ import (
type Filtering struct {
Path []string `arg optional placeholder:"<glob>"`
Match string `short:"m" placeholder:"<query>" help:"Terms to search for in the notes"`
Limit int `short:"n" placeholder:"<count>" help:"Limit the number of results"`
Created string ` placeholder:"<date>" help:"Only the notes created on the given date"`
CreatedBefore string ` placeholder:"<date>" help:"Only the notes created before the given date"`
CreatedAfter string ` placeholder:"<date>" help:"Only the notes created after the given date"`
Modified string ` placeholder:"<date>" help:"Only the notes modified on the given date"`
ModifiedBefore string ` placeholder:"<date>" help:"Only the notes modified before the given date"`
ModifiedAfter string ` placeholder:"<date>" help:"Only the notes modified after the given date"`
Related []string ` help:"Only the notes which might be related to the given notes" xor:"link"`
LinkedBy []string `short:"l" placeholder:"<path>" help:"Only the notes linked by the given notes" xor:"link"`
LinkingTo []string `short:"L" placeholder:"<path>" help:"Only the notes linking to the given notes" xor:"link"`
NotLinkedBy []string ` placeholder:"<path>" help:"Only the notes not linked by the given notes" xor:"link"`
NotLinkingTo []string ` placeholder:"<path>" help:"Only the notes not linking to the given notes" xor:"link"`
MaxDistance int ` help:"Maximum distance between two linked notes"`
Orphan bool ` help:"Only the notes which don't have any other note linking to them"`
Exclude []string `short:"x" placeholder:"<glob>" help:"Excludes notes matching the given file path pattern from the list"`
Recursive bool `short:"r" help:"Follow links recursively"`
Interactive bool `short:"i" help:"Further filter the list of notes interactively"`
Match string `short:"m" placeholder:"<query>" help:"Terms to search for in the notes."`
Limit int `short:"n" placeholder:"<count>" help:"Limit the number of results."`
Created string ` placeholder:"<date>" help:"Only the notes created on the given date."`
CreatedBefore string ` placeholder:"<date>" help:"Only the notes created before the given date."`
CreatedAfter string ` placeholder:"<date>" help:"Only the notes created after the given date."`
Modified string ` placeholder:"<date>" help:"Only the notes modified on the given date."`
ModifiedBefore string ` placeholder:"<date>" help:"Only the notes modified before the given date."`
ModifiedAfter string ` placeholder:"<date>" help:"Only the notes modified after the given date."`
Related []string ` help:"Only the notes which might be related to the given notes." xor:"link"`
LinkedBy []string `short:"l" placeholder:"<path>" help:"Only the notes linked by the given notes." xor:"link"`
LinkingTo []string `short:"L" placeholder:"<path>" help:"Only the notes linking to the given notes." xor:"link"`
NotLinkedBy []string ` placeholder:"<path>" help:"Only the notes not linked by the given notes." xor:"link"`
NotLinkingTo []string ` placeholder:"<path>" help:"Only the notes not linking to the given notes." xor:"link"`
MaxDistance int ` help:"Maximum distance between two linked notes."`
Orphan bool ` help:"Only the notes which don't have any other note linking to them."`
Exclude []string `short:"x" placeholder:"<glob>" help:"Excludes notes matching the given file path pattern from the list."`
Recursive bool `short:"r" help:"Follow links recursively."`
Interactive bool `short:"i" help:"Further filter the list of notes interactively."`
}
// Sorting holds sorting options to order notes.
type Sorting struct {
Sort []string `short:"s" placeholder:"<term>" help:"Sort the notes by the given criterion"`
Sort []string `short:"s" placeholder:"<term>" help:"Sort the notes by the given criterion."`
}
// NewFinderOpts creates an instance of note.FinderOpts from a set of user flags.

@ -13,8 +13,8 @@ import (
// Index indexes the content of all the notes in the slip box.
type Index struct {
Force bool `help:"Force indexing all the notes" short:"f"`
Quiet bool `help:"Do not print statistics nor progress" short:"q"`
Force bool `short:"f" help:"Force indexing all the notes."`
Quiet bool `short:"q" help:"Do not print statistics nor progress."`
}
func (cmd *Index) Run(container *Container) error {

@ -9,7 +9,7 @@ import (
// Init creates a slip box in the given directory
type Init struct {
Directory string `arg optional type:"path" default:"." help:"Directory containing the slip box"`
Directory string `arg optional type:"path" default:"." help:"Directory containing the slip box."`
}
func (cmd *Init) Run() error {

@ -15,11 +15,11 @@ import (
// List displays notes matching a set of criteria.
type List struct {
Format string `help:"Pretty prints the list using the given format" short:"f" placeholder:"<template>"`
NoPager bool `help:"Do not pipe zk output into a pager" short:"P"`
Quiet bool `help:"Don't show anything besides the notes themselves" short:"q"`
Format string `help:"Pretty prints the list using the given format." short:"f" placeholder:"<template>"`
NoPager bool `help:"Do not pipe zk output into a pager." short:"P"`
Quiet bool `help:"Don't show anything besides the notes themselves." short:"q"`
Delimiter string `default:"
" help:"Delimiter separating each result" short:"d"`
" help:"Delimiter separating each result." short:"d"`
Filtering
Sorting
}

@ -11,11 +11,12 @@ import (
// New adds a new note to the slip box.
type New struct {
Directory string `arg optional type:"path" default:"." help:"Directory in which to create the note"`
PrintPath bool `help:"Prints the path of the created note to stdin instead of editing it" short:"p"`
Title string `short:"t" help:"Title of the new note" placeholder:"<title>"`
Template string `type:"path" help:"Custom template to use to render the note" placeholder:"<path>"`
Extra map[string]string `help:"Extra variables passed to the templates"`
Directory string `arg optional type:"path" default:"." help:"Directory in which to create the note."`
PrintPath bool `short:"p" help:"Prints the path of the created note to stdin instead of editing it."`
Title string `short:"t" placeholder:"<title>" help:"Title of the new note."`
Template string `type:"path" placeholder:"<path>" help:"Custom template to use to render the note."`
Extra map[string]string ` help:"Extra variables passed to the templates."`
}
func (cmd *New) ConfigOverrides() zk.ConfigOverrides {

@ -14,13 +14,14 @@ var Version = "dev"
var Build = "dev"
var cli struct {
Index cmd.Index `cmd help:"Index the notes in the given directory to be searchable"`
Init cmd.Init `cmd help:"Create a slip box in the given directory"`
List cmd.List `cmd help:"List notes matching given criteria"`
Edit cmd.Edit `cmd help:"Edit notes matching given criteria"`
New cmd.New `cmd help:"Create a new note in the given slip box directory"`
NoInput NoInput `help:"Never prompt or ask for confirmation"`
Version kong.VersionFlag `help:"Print zk version"`
Index cmd.Index `cmd help:"Index the notes in the given directory to be searchable."`
Init cmd.Init `cmd help:"Create a slip box in the given directory."`
List cmd.List `cmd help:"List notes matching given criteria."`
Edit cmd.Edit `cmd help:"Edit notes matching given criteria."`
New cmd.New `cmd help:"Create a new note in the given slip box directory."`
NoInput NoInput `help:"Never prompt or ask for confirmation."`
Version kong.VersionFlag `help:"Print zk version."`
ShowHelp ShowHelp `cmd default:"1" hidden:true`
}
// NoInput is a flag preventing any user prompt when enabled.
@ -31,6 +32,21 @@ func (f NoInput) BeforeApply(container *cmd.Container) error {
return nil
}
// ShowHelp is the default command run. It's equivalent to `zk --help`.
type ShowHelp struct{}
func (cmd *ShowHelp) Run(container *cmd.Container) error {
parser, err := kong.New(&cli, options(container)...)
if err != nil {
return err
}
ctx, err := parser.Parse([]string{"--help"})
if err != nil {
return err
}
return ctx.Run(container)
}
func main() {
// Create the dependency graph.
container := cmd.NewContainer()
@ -41,19 +57,26 @@ func main() {
fatalIfError(err)
} else {
ctx := kong.Parse(&cli,
kong.Bind(container),
kong.Name("zk"),
kong.Vars{
"version": Version,
},
)
ctx := kong.Parse(&cli, options(container)...)
err := ctx.Run(container)
ctx.FatalIfErrorf(err)
}
}
func options(container *cmd.Container) []kong.Option {
return []kong.Option{
kong.Bind(container),
kong.Name("zk"),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
kong.Vars{
"version": Version,
},
}
}
func fatalIfError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "zk: error: %v\n", err)
@ -76,7 +99,7 @@ func runAlias(container *cmd.Container, args []string) (bool, error) {
continue
}
cmd := executil.CommandFromString(cmdStr, args...)
cmd := executil.CommandFromString(cmdStr, args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Loading…
Cancel
Save