main.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Command-line tool for creating and accessing backups.
  3. Usage:
  4. $ kopia [<flags>] <subcommand> [<args> ...]
  5. Use 'kopia help' to see more details.
  6. */
  7. package main
  8. import (
  9. "os"
  10. "github.com/alecthomas/kingpin/v2"
  11. "github.com/kopia/kopia/cli"
  12. "github.com/kopia/kopia/internal/logfile"
  13. "github.com/kopia/kopia/repo"
  14. )
  15. const usageTemplate = `{{define "FormatCommand" -}}
  16. {{if .FlagSummary}} {{.FlagSummary -}}{{end -}}
  17. {{range .Args}} {{if not .Required}}[{{end -}}<{{.Name -}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end -}}
  18. {{end -}}
  19. {{define "FormatCommandList" -}}
  20. {{range . -}}
  21. {{if not .Hidden -}}
  22. {{.Depth|Indent -}}{{.Name -}}{{if .Default -}}*{{end -}}{{template "FormatCommand" .}}
  23. {{template "FormatCommandList" .Commands -}}
  24. {{end -}}
  25. {{end -}}
  26. {{end -}}
  27. {{define "FormatUsage" -}}
  28. {{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end -}}
  29. {{if .Help}}
  30. {{.Help|Wrap 0 -}}
  31. {{end -}}
  32. {{end}}
  33. {{if .Context.SelectedCommand -}}
  34. usage: {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatUsage" .Context.SelectedCommand}}
  35. {{else -}}
  36. usage: {{.App.Name}}{{template "FormatUsage" .App}}
  37. {{end -}}
  38. {{if .Context.Flags -}}
  39. Flags:
  40. {{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}}
  41. {{end -}}
  42. {{if .Context.Args -}}
  43. Args:
  44. {{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
  45. {{end -}}
  46. {{if .Context.SelectedCommand -}}
  47. {{if .Context.SelectedCommand.Commands -}}
  48. Subcommands:
  49. {{.Context.SelectedCommand}}
  50. {{template "FormatCommandList" .Context.SelectedCommand.Commands -}}
  51. {{end -}}
  52. {{else if .App.Commands -}}
  53. Commands (use --help-full to list all commands):
  54. {{template "FormatCommandList" .App.Commands -}}
  55. {{end -}}
  56. `
  57. func main() {
  58. app := cli.NewApp()
  59. kp := kingpin.New("kopia", "Kopia - Fast And Secure Open-Source Backup").Author("http://kopia.github.io/")
  60. kp.Version(repo.BuildVersion + " build: " + repo.BuildInfo + " from: " + repo.BuildGitHubRepo)
  61. logfile.Attach(app, kp)
  62. kp.ErrorWriter(os.Stderr)
  63. kp.UsageWriter(os.Stdout)
  64. kp.UsageTemplate(usageTemplate)
  65. app.Attach(kp)
  66. kingpin.MustParse(kp.Parse(os.Args[1:]))
  67. }