0000: 2f 2f 20 43 4f 50 59 52 49 47 48 54 28 43 29 20 // COPYRIGHT(C)
0010: 44 61 76 69 64 20 4e 2e 20 43 6f 68 65 6e 2e 0a David N. Cohen..
0020: 2f 2f 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 // This file is
0030: 70 61 72 74 20 6f 66 20 73 72 63 2e 64 31 30 2e part of src.d10.
0040: 64 65 76 2f 63 6f 6d 6d 61 6e 64 0a 2f 2f 0a 2f dev/command.//./
0050: 2f 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 / This program i
0060: 73 20 66 72 65 65 20 73 6f 66 74 77 61 72 65 3a s free software:
0070: 20 79 6f 75 20 63 61 6e 20 72 65 64 69 73 74 72 you can redistr
0080: 69 62 75 74 65 20 69 74 20 61 6e 64 2f 6f 72 20 ibute it and/or
0090: 6d 6f 64 69 66 79 0a 2f 2f 20 69 74 20 75 6e 64 modify.// it und
00a0: 65 72 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 er the terms of
00b0: 74 68 65 20 47 4e 55 20 41 66 66 65 72 6f 20 47 the GNU Affero G
00c0: 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 20 4c 69 eneral Public Li
00d0: 63 65 6e 73 65 20 61 73 20 70 75 62 6c 69 73 68 cense as publish
00e0: 65 64 20 62 79 0a 2f 2f 20 74 68 65 20 46 72 65 ed by.// the Fre
00f0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0100: 61 74 69 6f 6e 2c 20 65 69 74 68 65 72 20 76 65 ation, either ve
0110: 72 73 69 6f 6e 20 33 20 6f 66 20 74 68 65 20 4c rsion 3 of the L
0120: 69 63 65 6e 73 65 2c 20 6f 72 0a 2f 2f 20 28 61 icense, or.// (a
0130: 74 20 79 6f 75 72 20 6f 70 74 69 6f 6e 29 20 61 t your option) a
0140: 6e 79 20 6c 61 74 65 72 20 76 65 72 73 69 6f 6e ny later version
0150: 2e 0a 2f 2f 0a 2f 2f 20 54 68 69 73 20 70 72 6f ..//.// This pro
0160: 67 72 61 6d 20 69 73 20 64 69 73 74 72 69 62 75 gram is distribu
0170: 74 65 64 20 69 6e 20 74 68 65 20 68 6f 70 65 20 ted in the hope
0180: 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 that it will be
0190: 75 73 65 66 75 6c 2c 0a 2f 2f 20 62 75 74 20 57 useful,.// but W
01a0: 49 54 48 4f 55 54 20 41 4e 59 20 57 41 52 52 41 ITHOUT ANY WARRA
01b0: 4e 54 59 3b 20 77 69 74 68 6f 75 74 20 65 76 65 NTY; without eve
01c0: 6e 20 74 68 65 20 69 6d 70 6c 69 65 64 20 77 61 n the implied wa
01d0: 72 72 61 6e 74 79 20 6f 66 0a 2f 2f 20 4d 45 52 rranty of.// MER
01e0: 43 48 41 4e 54 41 42 49 4c 49 54 59 20 6f 72 20 CHANTABILITY or
01f0: 46 49 54 4e 45 53 53 20 46 4f 52 20 41 20 50 41 FITNESS FOR A PA
0200: 52 54 49 43 55 4c 41 52 20 50 55 52 50 4f 53 45 RTICULAR PURPOSE
0210: 2e 20 20 53 65 65 20 74 68 65 0a 2f 2f 20 47 4e . See the.// GN
0220: 55 20 41 66 66 65 72 6f 20 47 65 6e 65 72 61 6c U Affero General
0230: 20 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 Public License
0240: 66 6f 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 for more details
0250: 2e 0a 2f 2f 0a 2f 2f 20 59 6f 75 20 73 68 6f 75 ..//.// You shou
0260: 6c 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 ld have received
0270: 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 a copy of the G
0280: 4e 55 20 41 66 66 65 72 6f 20 47 65 6e 65 72 61 NU Affero Genera
0290: 6c 20 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 l Public License
02a0: 0a 2f 2f 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 .// along with t
02b0: 68 69 73 20 70 72 6f 67 72 61 6d 2e 20 20 49 66 his program. If
02c0: 20 6e 6f 74 2c 20 73 65 65 20 3c 68 74 74 70 73 not, see <https
02d0: 3a 2f 2f 77 77 77 2e 67 6e 75 2e 6f 72 67 2f 6c ://www.gnu.org/l
02e0: 69 63 65 6e 73 65 73 2f 3e 2e 0a 0a 70 61 63 6b icenses/>...pack
02f0: 61 67 65 20 63 6f 6d 6d 61 6e 64 0a 0a 69 6d 70 age command..imp
0300: 6f 72 74 20 28 0a 09 22 66 6c 61 67 22 0a 09 22 ort (.."flag".."
0310: 66 6d 74 22 0a 09 22 6c 6f 67 22 0a 09 22 6f 73 fmt".."log".."os
0320: 22 0a 09 22 73 6c 69 63 65 73 22 0a 09 22 73 74 ".."slices".."st
0330: 72 69 6e 67 73 22 0a 29 0a 0a 76 61 72 20 65 72 rings".)..var er
0340: 72 73 20 3d 20 30 20 2f 2f 20 72 75 6e 6e 69 6e rs = 0 // runnin
0350: 67 20 63 6f 75 6e 74 20 6f 66 20 65 72 72 6f 72 g count of error
0360: 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 0a 66 s encountered..f
0370: 75 6e 63 20 43 68 65 63 6b 28 65 72 72 20 65 72 unc Check(err er
0380: 72 6f 72 29 20 7b 0a 09 69 66 20 65 72 72 20 21 ror) {..if err !
0390: 3d 20 6e 69 6c 20 7b 0a 09 09 65 72 72 73 2b 2b = nil {...errs++
03a0: 0a 09 09 66 6d 74 2e 50 72 69 6e 74 6c 6e 28 65 ...fmt.Println(e
03b0: 72 72 2e 45 72 72 6f 72 28 29 29 0a 09 7d 0a 7d rr.Error())..}.}
03c0: 0a 0a 2f 2f 20 43 68 65 63 6b 46 61 74 61 6c 20 ..// CheckFatal
03d0: 65 78 69 74 73 20 77 69 74 68 20 65 72 72 6f 72 exits with error
03e0: 20 6d 65 73 73 61 67 65 2c 20 69 66 20 65 72 72 message, if err
03f0: 20 69 73 20 6e 6f 74 20 6e 69 6c 2e 0a 66 75 6e is not nil..fun
0400: 63 20 43 68 65 63 6b 46 61 74 61 6c 28 65 72 72 c CheckFatal(err
0410: 20 65 72 72 6f 72 29 20 7b 0a 09 69 66 20 65 72 error) {..if er
0420: 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 09 65 72 72 r != nil {...err
0430: 73 2b 2b 0a 09 09 66 6d 74 2e 50 72 69 6e 74 6c s++...fmt.Printl
0440: 6e 28 65 72 72 2e 45 72 72 6f 72 28 29 29 0a 09 n(err.Error())..
0450: 09 2f 2f 20 6c 6f 67 2e 50 61 6e 69 63 28 65 72 .// log.Panic(er
0460: 72 29 20 2f 2f 20 70 72 6f 64 75 63 65 20 73 74 r) // produce st
0470: 61 63 6b 20 74 72 61 63 65 20 28 76 65 72 62 6f ack trace (verbo
0480: 73 65 29 0a 09 09 6f 73 2e 45 78 69 74 28 31 29 se)...os.Exit(1)
0490: 0a 09 7d 0a 7d 0a 0a 2f 2f 20 43 68 65 63 6b 55 ..}.}..// CheckU
04a0: 73 61 67 65 20 77 72 69 74 65 73 20 75 73 61 67 sage writes usag
04b0: 65 20 61 6e 64 20 65 78 69 74 73 2c 20 69 66 20 e and exits, if
04c0: 65 72 72 20 69 73 20 6e 6f 74 20 6e 69 6c 2e 0a err is not nil..
04d0: 66 75 6e 63 20 43 68 65 63 6b 55 73 61 67 65 28 func CheckUsage(
04e0: 65 72 72 20 65 72 72 6f 72 29 20 7b 0a 09 69 66 err error) {..if
04f0: 20 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 09 err != nil {...
0500: 65 72 72 73 2b 2b 0a 09 09 66 6d 74 2e 50 72 69 errs++...fmt.Pri
0510: 6e 74 6c 6e 28 65 72 72 2e 45 72 72 6f 72 28 29 ntln(err.Error()
0520: 29 0a 09 09 66 6c 61 67 2e 55 73 61 67 65 28 29 )...flag.Usage()
0530: 0a 09 09 6f 73 2e 45 78 69 74 28 32 29 0a 09 7d ...os.Exit(2)..}
0540: 0a 7d 0a 0a 74 79 70 65 20 50 61 74 68 20 73 74 .}..type Path st
0550: 72 69 6e 67 0a 0a 74 79 70 65 20 52 6f 75 74 65 ring..type Route
0560: 20 73 74 72 75 63 74 20 7b 0a 09 64 65 73 63 72 struct {..descr
0570: 69 70 74 69 6f 6e 20 73 74 72 69 6e 67 0a 09 70 iption string..p
0580: 61 74 68 20 20 20 20 20 20 20 20 50 61 74 68 0a ath Path.
0590: 09 72 6f 75 74 65 20 20 20 20 20 20 20 6d 61 70 .route map
05a0: 5b 50 61 74 68 5d 2a 52 6f 75 74 65 20 2f 2f 20 [Path]*Route //
05b0: 73 75 62 2d 63 6f 6d 6d 61 6e 64 73 20 61 6e 64 sub-commands and
05c0: 20 73 75 62 2d 73 75 62 2d 63 6f 6d 6d 61 6e 64 sub-sub-command
05d0: 73 0a 09 73 79 6e 74 61 78 20 20 20 20 20 20 73 s..syntax s
05e0: 74 72 69 6e 67 0a 0a 09 2f 2f 20 61 20 72 6f 75 tring...// a rou
05f0: 74 65 20 69 73 20 65 69 74 68 65 72 20 61 6e 20 te is either an
0600: 6f 70 65 72 61 74 69 6f 6e 20 6f 72 20 61 20 28 operation or a (
0610: 73 75 62 29 20 63 6f 6d 6d 61 6e 64 0a 09 63 6f sub) command..co
0620: 6d 6d 61 6e 64 20 20 20 43 6f 6d 6d 61 6e 64 0a mmand Command.
0630: 09 6f 70 65 72 61 74 69 6f 6e 20 4f 70 65 72 61 .operation Opera
0640: 74 69 6f 6e 0a 7d 0a 0a 74 79 70 65 20 43 6f 6d tion.}..type Com
0650: 6d 61 6e 64 20 66 75 6e 63 28 29 20 4f 70 65 72 mand func() Oper
0660: 61 74 69 6f 6e 0a 0a 74 79 70 65 20 4f 70 65 72 ation..type Oper
0670: 61 74 69 6f 6e 20 66 75 6e 63 28 29 20 65 72 72 ation func() err
0680: 6f 72 0a 0a 66 75 6e 63 20 4e 65 77 28 70 20 50 or..func New(p P
0690: 61 74 68 2c 20 73 79 6e 74 61 78 20 73 74 72 69 ath, syntax stri
06a0: 6e 67 2c 20 64 65 73 63 72 69 70 74 69 6f 6e 20 ng, description
06b0: 73 74 72 69 6e 67 2c 20 63 20 43 6f 6d 6d 61 6e string, c Comman
06c0: 64 29 20 2a 52 6f 75 74 65 20 7b 0a 09 72 65 74 d) *Route {..ret
06d0: 75 72 6e 20 26 52 6f 75 74 65 7b 0a 09 09 63 6f urn &Route{...co
06e0: 6d 6d 61 6e 64 3a 20 20 20 20 20 63 2c 0a 09 09 mmand: c,...
06f0: 64 65 73 63 72 69 70 74 69 6f 6e 3a 20 73 74 72 description: str
0700: 69 6e 67 73 2e 54 72 69 6d 53 70 61 63 65 28 64 ings.TrimSpace(d
0710: 65 73 63 72 69 70 74 69 6f 6e 29 2c 0a 09 09 70 escription),...p
0720: 61 74 68 3a 20 20 20 20 20 20 20 20 70 2c 0a 09 ath: p,..
0730: 09 73 79 6e 74 61 78 3a 20 20 20 20 20 20 73 74 .syntax: st
0740: 72 69 6e 67 73 2e 54 72 69 6d 53 70 61 63 65 28 rings.TrimSpace(
0750: 73 79 6e 74 61 78 29 2c 0a 09 7d 0a 7d 0a 0a 66 syntax),..}.}..f
0760: 75 6e 63 20 28 72 20 2a 52 6f 75 74 65 29 20 4e unc (r *Route) N
0770: 65 77 28 70 20 50 61 74 68 2c 20 73 79 6e 74 61 ew(p Path, synta
0780: 78 20 73 74 72 69 6e 67 2c 20 64 65 73 63 72 69 x string, descri
0790: 70 74 69 6f 6e 20 73 74 72 69 6e 67 2c 20 63 20 ption string, c
07a0: 43 6f 6d 6d 61 6e 64 29 20 2a 52 6f 75 74 65 20 Command) *Route
07b0: 7b 0a 09 73 63 20 3a 3d 20 4e 65 77 28 70 2c 20 {..sc := New(p,
07c0: 73 79 6e 74 61 78 2c 20 64 65 73 63 72 69 70 74 syntax, descript
07d0: 69 6f 6e 2c 20 63 29 0a 09 72 2e 61 64 64 28 73 ion, c)..r.add(s
07e0: 63 29 0a 09 72 65 74 75 72 6e 20 73 63 0a 7d 0a c)..return sc.}.
07f0: 0a 66 75 6e 63 20 28 72 20 2a 52 6f 75 74 65 29 .func (r *Route)
0800: 20 4f 70 65 72 61 74 69 6f 6e 28 70 20 50 61 74 Operation(p Pat
0810: 68 2c 20 73 79 6e 74 61 78 20 73 74 72 69 6e 67 h, syntax string
0820: 2c 20 64 65 73 63 72 69 70 74 69 6f 6e 20 73 74 , description st
0830: 72 69 6e 67 2c 20 6f 20 4f 70 65 72 61 74 69 6f ring, o Operatio
0840: 6e 29 20 7b 0a 09 6f 70 20 3a 3d 20 26 52 6f 75 n) {..op := &Rou
0850: 74 65 7b 0a 09 09 64 65 73 63 72 69 70 74 69 6f te{...descriptio
0860: 6e 3a 20 73 74 72 69 6e 67 73 2e 54 72 69 6d 53 n: strings.TrimS
0870: 70 61 63 65 28 64 65 73 63 72 69 70 74 69 6f 6e pace(description
0880: 29 2c 0a 09 09 6f 70 65 72 61 74 69 6f 6e 3a 20 ),...operation:
0890: 20 20 6f 2c 0a 09 09 70 61 74 68 3a 20 20 20 20 o,...path:
08a0: 20 20 20 20 70 2c 0a 09 09 73 79 6e 74 61 78 3a p,...syntax:
08b0: 20 20 20 20 20 20 73 74 72 69 6e 67 73 2e 54 72 strings.Tr
08c0: 69 6d 53 70 61 63 65 28 73 79 6e 74 61 78 29 2c imSpace(syntax),
08d0: 0a 09 7d 0a 09 72 2e 61 64 64 28 6f 70 29 0a 7d ..}..r.add(op).}
08e0: 0a 0a 66 75 6e 63 20 28 72 20 2a 52 6f 75 74 65 ..func (r *Route
08f0: 29 20 61 64 64 28 6f 70 20 2a 52 6f 75 74 65 29 ) add(op *Route)
0900: 20 7b 0a 09 69 66 20 72 2e 72 6f 75 74 65 20 3d {..if r.route =
0910: 3d 20 6e 69 6c 20 7b 0a 09 09 72 2e 72 6f 75 74 = nil {...r.rout
0920: 65 20 3d 20 6d 61 70 5b 50 61 74 68 5d 2a 52 6f e = map[Path]*Ro
0930: 75 74 65 7b 7d 0a 09 7d 0a 09 5f 2c 20 6f 6b 20 ute{}..}.._, ok
0940: 3a 3d 20 72 2e 72 6f 75 74 65 5b 6f 70 2e 70 61 := r.route[op.pa
0950: 74 68 5d 0a 09 69 66 20 6f 6b 20 7b 0a 09 09 6c th]..if ok {...l
0960: 6f 67 2e 50 61 6e 69 63 66 28 22 64 75 70 6c 69 og.Panicf("dupli
0970: 63 61 74 65 20 72 6f 75 74 65 20 28 25 71 29 22 cate route (%q)"
0980: 2c 20 6f 70 2e 70 61 74 68 29 0a 09 7d 0a 09 72 , op.path)..}..r
0990: 2e 72 6f 75 74 65 5b 6f 70 2e 70 61 74 68 5d 20 .route[op.path]
09a0: 3d 20 6f 70 0a 7d 0a 0a 66 75 6e 63 20 28 72 20 = op.}..func (r
09b0: 52 6f 75 74 65 29 20 52 75 6e 28 29 20 65 72 72 Route) Run() err
09c0: 6f 72 20 7b 0a 09 69 66 20 72 2e 6f 70 65 72 61 or {..if r.opera
09d0: 74 69 6f 6e 20 21 3d 20 6e 69 6c 20 7b 0a 09 09 tion != nil {...
09e0: 72 65 74 75 72 6e 20 72 2e 6f 70 65 72 61 74 69 return r.operati
09f0: 6f 6e 28 29 0a 09 7d 0a 0a 09 2f 2f 20 64 65 74 on()..}...// det
0a00: 65 72 6d 69 6e 65 20 74 68 65 20 64 65 66 61 75 ermine the defau
0a10: 6c 74 20 6f 70 65 72 61 74 69 6f 6e 2c 20 65 76 lt operation, ev
0a20: 65 6e 20 69 66 20 77 65 20 64 6f 20 6e 6f 74 20 en if we do not
0a30: 72 75 6e 20 74 68 65 0a 09 2f 2f 20 64 65 66 61 run the..// defa
0a40: 75 6c 74 2c 20 74 68 69 73 20 67 69 76 65 73 20 ult, this gives
0a50: 74 68 65 20 63 6f 6d 6d 61 6e 64 20 68 61 6e 64 the command hand
0a60: 6c 65 72 20 61 20 63 68 61 6e 63 65 20 74 6f 20 ler a chance to
0a70: 69 6e 69 74 69 61 6c 69 7a 65 0a 09 2f 2f 20 76 initialize..// v
0a80: 61 72 69 61 62 6c 65 73 20 61 6e 64 20 64 65 63 ariables and dec
0a90: 6c 61 72 65 20 66 6c 61 67 73 2e 0a 09 64 65 66 lare flags...def
0aa0: 61 75 6c 74 4f 70 20 3a 3d 20 72 2e 63 6f 6d 6d aultOp := r.comm
0ab0: 61 6e 64 28 29 0a 0a 09 2f 2f 20 66 6c 61 67 20 and()...// flag
0ac0: 70 61 63 6b 61 67 65 20 6d 61 73 6b 73 20 74 68 package masks th
0ad0: 65 20 73 70 65 63 69 61 6c 20 61 72 67 20 22 2d e special arg "-
0ae0: 2d 22 2c 20 73 6f 20 68 65 72 65 20 77 65 20 72 -", so here we r
0af0: 65 6d 65 6d 62 65 72 20 77 68 61 74 20 63 6f 6d emember what com
0b00: 65 73 20 61 66 74 65 72 20 69 74 0a 09 73 65 70 es after it..sep
0b10: 61 72 61 74 6f 72 20 3a 3d 20 73 6c 69 63 65 73 arator := slices
0b20: 2e 49 6e 64 65 78 28 6f 73 2e 41 72 67 73 2c 20 .Index(os.Args,
0b30: 22 2d 2d 22 29 0a 09 76 61 72 20 65 78 74 72 61 "--")..var extra
0b40: 20 5b 5d 73 74 72 69 6e 67 20 2f 2f 20 61 72 67 []string // arg
0b50: 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 22 2d s that follow "-
0b60: 2d 22 0a 09 69 66 20 73 65 70 61 72 61 74 6f 72 -"..if separator
0b70: 20 21 3d 20 2d 31 20 7b 0a 09 09 65 78 74 72 61 != -1 {...extra
0b80: 20 3d 20 6f 73 2e 41 72 67 73 5b 73 65 70 61 72 = os.Args[separ
0b90: 61 74 6f 72 3a 5d 0a 09 09 6f 73 2e 41 72 67 73 ator:]...os.Args
0ba0: 20 3d 20 6f 73 2e 41 72 67 73 5b 3a 73 65 70 61 = os.Args[:sepa
0bb0: 72 61 74 6f 72 5d 0a 09 09 2f 2f 20 73 65 65 20 rator]...// see
0bc0: 61 64 64 69 74 69 6f 6e 61 6c 20 6d 61 6e 69 70 additional manip
0bd0: 75 6c 61 74 69 6f 6e 20 6f 66 20 61 72 67 73 20 ulation of args
0be0: 6e 65 61 72 20 65 6e 64 20 6f 66 20 74 68 69 73 near end of this
0bf0: 20 66 75 6e 63 74 69 6f 6e 0a 09 7d 0a 0a 09 2f function..}.../
0c00: 2f 20 70 61 72 73 65 2c 20 69 6e 20 6f 72 64 65 / parse, in orde
0c10: 72 20 74 6f 20 6e 61 76 69 67 61 74 65 20 66 75 r to navigate fu
0c20: 72 74 68 65 72 20 64 6f 77 6e 20 72 6f 75 74 65 rther down route
0c30: 0a 09 66 6c 61 67 2e 55 73 61 67 65 20 3d 20 72 ..flag.Usage = r
0c40: 2e 75 73 61 67 65 0a 09 66 6c 61 67 2e 50 61 72 .usage..flag.Par
0c50: 73 65 28 29 0a 0a 09 2f 2f 20 52 65 73 74 6f 72 se()...// Restor
0c60: 65 20 65 78 74 72 61 20 6f 73 2e 41 72 67 73 2c e extra os.Args,
0c70: 20 69 6e 20 63 61 73 65 20 64 65 66 61 75 6c 74 in case default
0c80: 4f 70 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c 73 Op handler calls
0c90: 0a 09 2f 2f 20 66 6c 61 67 2e 50 61 72 73 65 28 ..// flag.Parse(
0ca0: 29 2e 20 53 65 65 20 61 6c 73 6f 20 77 68 65 72 ). See also wher
0cb0: 65 20 77 65 20 64 65 73 63 65 6e 64 28 29 20 62 e we descend() b
0cc0: 65 6c 6f 77 2e 0a 09 6f 73 2e 41 72 67 73 20 3d elow...os.Args =
0cd0: 20 61 70 70 65 6e 64 28 6f 73 2e 41 72 67 73 2c append(os.Args,
0ce0: 20 65 78 74 72 61 2e 2e 2e 29 0a 0a 09 76 61 72 extra...)...var
0cf0: 20 70 61 74 68 20 50 61 74 68 0a 09 61 72 67 20 path Path..arg
0d00: 3a 3d 20 66 6c 61 67 2e 41 72 67 73 28 29 0a 09 := flag.Args()..
0d10: 69 66 20 6c 65 6e 28 61 72 67 29 20 3e 20 30 20 if len(arg) > 0
0d20: 7b 0a 09 09 70 61 74 68 20 3d 20 50 61 74 68 28 {...path = Path(
0d30: 61 72 67 5b 30 5d 29 0a 09 7d 0a 0a 09 6e 65 78 arg[0])..}...nex
0d40: 74 2c 20 6f 6b 20 3a 3d 20 72 2e 72 6f 75 74 65 t, ok := r.route
0d50: 5b 70 61 74 68 5d 0a 09 69 66 20 21 6f 6b 20 26 [path]..if !ok &
0d60: 26 20 64 65 66 61 75 6c 74 4f 70 20 21 3d 20 6e & defaultOp != n
0d70: 69 6c 20 7b 0a 09 09 72 65 74 75 72 6e 20 64 65 il {...return de
0d80: 66 61 75 6c 74 4f 70 28 29 0a 09 7d 0a 09 69 66 faultOp()..}..if
0d90: 20 21 6f 6b 20 26 26 20 70 61 74 68 20 21 3d 20 !ok && path !=
0da0: 22 22 20 7b 0a 09 09 65 72 72 20 3a 3d 20 66 6d "" {...err := fm
0db0: 74 2e 45 72 72 6f 72 66 28 22 75 6e 6b 6e 6f 77 t.Errorf("unknow
0dc0: 6e 20 6f 70 65 72 61 74 69 6f 6e 20 28 25 71 29 n operation (%q)
0dd0: 22 2c 20 70 61 74 68 29 0a 09 09 43 68 65 63 6b ", path)...Check
0de0: 55 73 61 67 65 28 65 72 72 29 0a 09 7d 0a 09 69 Usage(err)..}..i
0df0: 66 20 21 6f 6b 20 7b 0a 09 09 72 2e 75 73 61 67 f !ok {...r.usag
0e00: 65 28 29 0a 09 09 6f 73 2e 45 78 69 74 28 32 29 e()...os.Exit(2)
0e10: 0a 09 7d 0a 0a 09 6f 73 2e 41 72 67 73 2c 20 66 ..}...os.Args, f
0e20: 6c 61 67 2e 43 6f 6d 6d 61 6e 64 4c 69 6e 65 20 lag.CommandLine
0e30: 3d 20 6e 65 78 74 2e 64 65 73 63 65 6e 64 28 29 = next.descend()
0e40: 0a 0a 09 2f 2f 20 72 65 73 74 6f 72 65 20 65 78 ...// restore ex
0e50: 74 72 61 20 6f 73 2e 41 72 67 73 2c 20 62 65 63 tra os.Args, bec
0e60: 61 75 73 65 20 74 68 65 79 20 77 65 72 65 20 72 ause they were r
0e70: 65 6d 6f 76 65 64 20 77 68 65 6e 20 66 6c 61 67 emoved when flag
0e80: 2e 50 61 72 73 65 20 77 61 73 20 63 61 6c 6c 65 .Parse was calle
0e90: 64 0a 09 6f 73 2e 41 72 67 73 20 3d 20 61 70 70 d..os.Args = app
0ea0: 65 6e 64 28 6f 73 2e 41 72 67 73 2c 20 65 78 74 end(os.Args, ext
0eb0: 72 61 2e 2e 2e 29 0a 0a 09 72 65 74 75 72 6e 20 ra...)...return
0ec0: 6e 65 78 74 2e 52 75 6e 28 29 0a 7d 0a 0a 66 75 next.Run().}..fu
0ed0: 6e 63 20 28 72 20 52 6f 75 74 65 29 20 64 65 73 nc (r Route) des
0ee0: 63 65 6e 64 28 29 20 28 61 72 67 20 5b 5d 73 74 cend() (arg []st
0ef0: 72 69 6e 67 2c 20 66 73 20 2a 66 6c 61 67 2e 46 ring, fs *flag.F
0f00: 6c 61 67 53 65 74 29 20 7b 0a 09 69 66 20 66 6c lagSet) {..if fl
0f10: 61 67 2e 4e 41 72 67 28 29 20 3e 20 30 20 7b 0a ag.NArg() > 0 {.
0f20: 09 09 61 72 67 20 3d 20 66 6c 61 67 2e 41 72 67 ..arg = flag.Arg
0f30: 73 28 29 0a 09 7d 0a 09 66 73 20 3d 20 66 6c 61 s()..}..fs = fla
0f40: 67 2e 4e 65 77 46 6c 61 67 53 65 74 28 73 74 72 g.NewFlagSet(str
0f50: 69 6e 67 28 72 2e 70 61 74 68 29 2c 20 66 6c 61 ing(r.path), fla
0f60: 67 2e 45 78 69 74 4f 6e 45 72 72 6f 72 29 0a 09 g.ExitOnError)..
0f70: 66 73 2e 55 73 61 67 65 20 3d 20 72 2e 75 73 61 fs.Usage = r.usa
0f80: 67 65 0a 0a 09 2f 2f 20 69 6e 6a 65 63 74 20 66 ge...// inject f
0f90: 6c 61 67 73 20 61 6c 72 65 61 64 79 20 70 61 72 lags already par
0fa0: 73 65 64 20 69 6e 74 6f 20 6f 75 72 20 66 6c 61 sed into our fla
0fb0: 67 73 65 74 0a 09 66 6c 61 67 2e 56 69 73 69 74 gset..flag.Visit
0fc0: 41 6c 6c 28 66 75 6e 63 28 66 20 2a 66 6c 61 67 All(func(f *flag
0fd0: 2e 46 6c 61 67 29 20 7b 0a 09 09 66 73 2e 56 61 .Flag) {...fs.Va
0fe0: 72 28 66 2e 56 61 6c 75 65 2c 20 66 2e 4e 61 6d r(f.Value, f.Nam
0ff0: 65 2c 20 66 2e 55 73 61 67 65 29 0a 09 7d 29 0a e, f.Usage)..}).
1000: 0a 09 72 65 74 75 72 6e 0a 7d 0a 0a 66 75 6e 63 ..return.}..func
1010: 20 28 72 20 52 6f 75 74 65 29 20 75 73 61 67 65 (r Route) usage
1020: 28 29 20 7b 0a 09 6f 75 74 70 75 74 20 3a 3d 20 () {..output :=
1030: 66 6c 61 67 2e 43 6f 6d 6d 61 6e 64 4c 69 6e 65 flag.CommandLine
1040: 2e 4f 75 74 70 75 74 28 29 0a 0a 09 69 66 20 65 .Output()...if e
1050: 72 72 73 20 3d 3d 20 30 20 26 26 20 72 2e 64 65 rrs == 0 && r.de
1060: 73 63 72 69 70 74 69 6f 6e 20 21 3d 20 22 22 20 scription != ""
1070: 7b 0a 09 09 66 6d 74 2e 46 70 72 69 6e 74 66 28 {...fmt.Fprintf(
1080: 6f 75 74 70 75 74 2c 20 22 5c 6e 22 2b 72 2e 64 output, "\n"+r.d
1090: 65 73 63 72 69 70 74 69 6f 6e 2b 22 5c 6e 22 29 escription+"\n")
10a0: 0a 09 7d 0a 09 69 66 20 72 2e 73 79 6e 74 61 78 ..}..if r.syntax
10b0: 20 21 3d 20 22 22 20 7b 0a 09 09 66 6d 74 2e 46 != "" {...fmt.F
10c0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
10d0: 5c 6e 53 79 6e 74 61 78 3a 5c 6e 22 29 0a 09 09 \nSyntax:\n")...
10e0: 66 6d 74 2e 46 70 72 69 6e 74 66 28 6f 75 74 70 fmt.Fprintf(outp
10f0: 75 74 2c 20 22 5c 6e 20 20 25 73 22 2c 20 72 2e ut, "\n %s", r.
1100: 73 79 6e 74 61 78 29 0a 09 09 66 6d 74 2e 46 70 syntax)...fmt.Fp
1110: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
1120: 6e 22 29 0a 09 7d 0a 0a 09 69 66 20 6c 65 6e 28 n")..}...if len(
1130: 72 2e 72 6f 75 74 65 29 20 3e 20 30 20 7b 0a 09 r.route) > 0 {..
1140: 09 66 6d 74 2e 46 70 72 69 6e 74 66 28 6f 75 74 .fmt.Fprintf(out
1150: 70 75 74 2c 20 22 5c 6e 4f 70 65 72 61 74 69 6f put, "\nOperatio
1160: 6e 73 3a 5c 6e 22 29 0a 09 09 66 6f 72 20 70 2c ns:\n")...for p,
1170: 20 6f 70 20 3a 3d 20 72 61 6e 67 65 20 72 2e 72 op := range r.r
1180: 6f 75 74 65 20 7b 0a 09 09 09 73 79 6e 74 61 78 oute {....syntax
1190: 20 3a 3d 20 6f 70 2e 73 79 6e 74 61 78 0a 09 09 := op.syntax...
11a0: 09 69 66 20 73 79 6e 74 61 78 20 3d 3d 20 22 22 .if syntax == ""
11b0: 20 7b 0a 09 09 09 09 73 79 6e 74 61 78 20 3d 20 {.....syntax =
11c0: 73 74 72 69 6e 67 28 70 29 0a 09 09 09 7d 0a 0a string(p)....}..
11d0: 09 09 09 66 6d 74 2e 46 70 72 69 6e 74 66 28 6f ...fmt.Fprintf(o
11e0: 75 74 70 75 74 2c 20 22 5c 6e 20 20 25 73 22 2c utput, "\n %s",
11f0: 20 73 79 6e 74 61 78 29 0a 09 09 09 69 66 20 6f syntax)....if o
1200: 70 2e 64 65 73 63 72 69 70 74 69 6f 6e 20 21 3d p.description !=
1210: 20 22 22 20 7b 0a 09 09 09 09 66 6d 74 2e 46 70 "" {.....fmt.Fp
1220: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
1230: 6e 5c 74 25 73 22 2c 20 66 69 72 73 74 4c 69 6e n\t%s", firstLin
1240: 65 28 6f 70 2e 64 65 73 63 72 69 70 74 69 6f 6e e(op.description
1250: 29 29 0a 09 09 09 7d 0a 09 09 09 66 6d 74 2e 46 ))....}....fmt.F
1260: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
1270: 5c 6e 22 29 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2f \n")...}..}...//
1280: 20 73 68 6f 77 20 66 6c 61 67 73 20 6f 6e 6c 79 show flags only
1290: 20 69 66 20 73 6f 6d 65 20 68 61 76 65 20 62 65 if some have be
12a0: 65 6e 20 64 65 63 6c 61 72 65 64 0a 09 66 6c 61 en declared..fla
12b0: 67 73 20 3a 3d 20 30 0a 09 66 6c 61 67 2e 56 69 gs := 0..flag.Vi
12c0: 73 69 74 41 6c 6c 28 66 75 6e 63 28 2a 66 6c 61 sitAll(func(*fla
12d0: 67 2e 46 6c 61 67 29 20 7b 0a 09 09 66 6c 61 67 g.Flag) {...flag
12e0: 73 2b 2b 0a 09 7d 29 0a 09 69 66 20 66 6c 61 67 s++..})..if flag
12f0: 73 20 3e 20 30 20 7b 0a 09 09 66 6d 74 2e 46 70 s > 0 {...fmt.Fp
1300: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c rintf(output, "\
1310: 6e 46 6c 61 67 73 3a 5c 6e 5c 6e 22 29 0a 09 09 nFlags:\n\n")...
1320: 66 6c 61 67 2e 50 72 69 6e 74 44 65 66 61 75 6c flag.PrintDefaul
1330: 74 73 28 29 0a 09 7d 0a 09 66 6d 74 2e 50 72 69 ts()..}..fmt.Pri
1340: 6e 74 6c 6e 28 22 22 29 20 2f 2f 20 62 6c 61 6e ntln("") // blan
1350: 6b 20 6c 69 6e 65 0a 7d 0a 0a 66 75 6e 63 20 66 k line.}..func f
1360: 69 72 73 74 4c 69 6e 65 28 74 78 74 20 73 74 72 irstLine(txt str
1370: 69 6e 67 29 20 73 74 72 69 6e 67 20 7b 0a 09 73 ing) string {..s
1380: 70 6c 69 74 20 3a 3d 20 73 74 72 69 6e 67 73 2e plit := strings.
1390: 53 70 6c 69 74 4e 28 74 78 74 2c 20 22 5c 6e 22 SplitN(txt, "\n"
13a0: 2c 20 32 29 0a 09 72 65 74 75 72 6e 20 73 70 6c , 2)..return spl
13b0: 69 74 5b 30 5d 0a 7d 0a it[0].}.