@@ -22,18 +22,13 @@ func RunQuery(c *cli.Context) error {
2222 return nil
2323 }
2424
25- // urfave/cli stops parsing flags at the first positional arg, so support a
26- // trailing --json too (e.g. `lrc query stats --json`).
25+ // Seed from flags placed BEFORE the positional arg (cli parses those).
2726 jsonOut := c .Bool ("json" )
28- positionals := make ([]string , 0 , c .NArg ())
29- for _ , a := range c .Args ().Slice () {
30- switch a {
31- case "--json" , "-json" , "-j" :
32- jsonOut = true
33- default :
34- positionals = append (positionals , a )
35- }
36- }
27+ filter := Filter {From : c .String ("from" ), To : c .String ("to" ), Range : c .String ("range" )}
28+
29+ // urfave/cli stops parsing flags at the first positional arg, so also scan
30+ // the remaining args for trailing flags (e.g. `lrc query stats --from 2024-01-01`).
31+ positionals := parseTrailingFlags (c .Args ().Slice (), & jsonOut , & filter )
3732
3833 arg := "stats" // default alias
3934 if len (positionals ) > 0 && strings .TrimSpace (positionals [0 ]) != "" {
@@ -49,7 +44,7 @@ func RunQuery(c *cli.Context) error {
4944 sqlText = strings .Join (positionals , " " )
5045 }
5146
52- res , err := Run (Filter {} , sqlText )
47+ res , err := Run (filter , sqlText )
5348 if err != nil {
5449 return err
5550 }
@@ -66,6 +61,50 @@ func RunQuery(c *cli.Context) error {
6661 return nil
6762}
6863
64+ // parseTrailingFlags pulls flags out of args that cli left unparsed (anything
65+ // after the first positional). Supports `--flag value` and `--flag=value`.
66+ // Returns the remaining positional args; sets jsonOut/filter via pointers.
67+ func parseTrailingFlags (args []string , jsonOut * bool , filter * Filter ) []string {
68+ positionals := make ([]string , 0 , len (args ))
69+ for i := 0 ; i < len (args ); i ++ {
70+ a := args [i ]
71+ // flag=value form
72+ switch {
73+ case a == "--json" || a == "-j" :
74+ * jsonOut = true
75+ continue
76+ case strings .HasPrefix (a , "--from=" ):
77+ filter .From = strings .TrimPrefix (a , "--from=" )
78+ continue
79+ case strings .HasPrefix (a , "--to=" ):
80+ filter .To = strings .TrimPrefix (a , "--to=" )
81+ continue
82+ case strings .HasPrefix (a , "--range=" ):
83+ filter .Range = strings .TrimPrefix (a , "--range=" )
84+ continue
85+ }
86+ // flag value form (consume next arg)
87+ if i + 1 < len (args ) {
88+ switch a {
89+ case "--from" :
90+ filter .From = args [i + 1 ]
91+ i ++
92+ continue
93+ case "--to" :
94+ filter .To = args [i + 1 ]
95+ i ++
96+ continue
97+ case "--range" :
98+ filter .Range = args [i + 1 ]
99+ i ++
100+ continue
101+ }
102+ }
103+ positionals = append (positionals , a )
104+ }
105+ return positionals
106+ }
107+
69108// RunQueryList prints every alias and its source.
70109func RunQueryList (c * cli.Context ) error {
71110 aliases , err := ListAliases ()
0 commit comments