44import java .util .ArrayList ;
55import java .util .LinkedHashMap ;
66import java .util .List ;
7- import java .util .Locale ;
87import java .util .Map ;
98import java .util .Objects ;
9+ import java .util .regex .Pattern ;
1010import lombok .extern .slf4j .Slf4j ;
1111
12- /**
13- * 将 {@link io.github.easy4j.opencli.core.OpenCliExecutor#invoke(List)} 使用的 argv 列表
14- *({@code [adapter, command, ...]}) best-effort 映射为 Agent {@link OpenCliCollectRequest}。
15- * 规则:前两个 token 为 {@code site}、{@code command};其后从左到右扫描:
16- * <ul>
17- * <li>{@code -f} / {@code --format} 及其下一 token 写入请求 {@code format},不进入 args;</li>
18- * <li>{@code --key=value} 或 {@code --key} + 下一非选项值 → 进入 {@code args}(key 无 {@code --} 前缀);</li>
19- * <li>单独 {@code --flag}(下一 token 以 {@code -} 开头或缺失)→ {@code args[flag]=true};</li>
20- * <li>其余 token 按顺序进入 {@code positional_args}。</li>
21- * </ul>
22- * Agent 会自行追加 {@code -f <format>},故本解析器会消费调用方已写的 {@code -f},避免重复。
23- */
24- @ Slf4j /**
25-
26- * Best-effort mapping from the argv list used by
27- * {@link io.github.easy4j.opencli.core.OpenCliExecutor#invoke(List)}
28- * ({@code [adapter, command, ...]}) to an Agent {@link OpenCliCollectRequest}.
29- *
30- * <p>Parsing rules: the first two tokens are {@code site} and {@code command};
31- * subsequent tokens are scanned left-to-right:</p>
32- * <ul>
33- * <li>{@code -f} / {@code --format} and its next token are written to {@code format}, not args;</li>
34- * <li>{@code --key=value} or {@code --key} + next non-option value go to {@code args} (key without {@code --} prefix);</li>
35- * <li>Standalone {@code --flag} (next token starts with {@code -} or is absent) goes to {@code args[flag]=true};</li>
36- * <li>All other tokens go to {@code positional_args} in order.</li>
37- * </ul>
38-
39- *
40-
41- * @author <a href="https://github.com/loong10k">Loong Wan</a>
42-
43- * @since 3.0.0
44-
45- */
46-
12+ /** Maps the supported adapter argv grammar to the legacy HTTP collect DTO. */
13+ @ Slf4j
4714public final class OpenCliArgvToCollectParser {
4815
16+ private static final Pattern IDENTIFIER = Pattern .compile ("[A-Za-z0-9][A-Za-z0-9_-]*" );
17+
4918 private OpenCliArgvToCollectParser () {
5019 }
5120
@@ -66,75 +35,53 @@ public static OpenCliCollectRequest parse(
6635 throw new IllegalArgumentException (
6736 "remote invoke requires at least [adapter, command], got " + adapterAndRest .size () + " tokens" );
6837 }
69- String site = adapterAndRest .get (0 ).trim ();
70- String command = adapterAndRest .get (1 ).trim ();
71- if (OpenCliStrings .isBlank (site ) || OpenCliStrings .isBlank (command )) {
72- throw new IllegalArgumentException ("adapter and command must not be blank" );
73- }
38+ String site = requireIdentifier (adapterAndRest .get (0 ));
39+ String command = requireIdentifier (adapterAndRest .get (1 ));
7440
7541 String format = OpenCliStrings .isNotBlank (defaultFormat ) ? defaultFormat .trim () : "json" ;
7642 String mode = OpenCliStrings .isNotBlank (defaultMode ) ? defaultMode .trim () : "cdp" ;
7743 Map <String , Object > args = new LinkedHashMap <>();
7844 List <String > positional = new ArrayList <>();
45+ boolean formatSpecified = false ;
7946
80- int i = 2 ;
81- while (i < adapterAndRest .size ()) {
82- String raw = adapterAndRest .get (i );
83- if (Objects .isNull (raw ) || raw .isEmpty ()) {
84- log .warn ("Skipping null or empty argv token at index={}" , i );
85- i ++;
86- continue ;
47+ int index = 2 ;
48+ while (index < adapterAndRest .size ()) {
49+ String token = adapterAndRest .get (index ++);
50+ if (Objects .isNull (token ) || token .isEmpty () || !token .equals (token .trim ())) {
51+ throw new IllegalArgumentException ("Legacy collect cannot preserve an empty or padded token" );
8752 }
88- String t = raw .trim ();
89- if (t .isEmpty ()) {
90- i ++;
91- continue ;
92- }
93-
94- if ("-f" .equals (t ) || "--format" .equals (t )) {
95- if (i + 1 >= adapterAndRest .size ()) {
96- throw new IllegalArgumentException ("-f requires a value" );
53+ if (!token .startsWith ("-" )) {
54+ if (formatSpecified || !args .isEmpty ()) {
55+ throw new IllegalArgumentException ("Legacy collect requires positional arguments before options" );
9756 }
98- format = Objects .requireNonNull (adapterAndRest .get (i + 1 )).trim ();
99- i += 2 ;
57+ positional .add (token );
10058 continue ;
10159 }
10260
103- if (t .startsWith ("--" )) {
104- if (t .contains ("=" )) {
105- int eq = t .indexOf ('=' );
106- String key = normalizeKey (t .substring (2 , eq ));
107- String val = t .substring (eq + 1 );
108- if (isFormatKey (key )) {
109- format = val ;
110- } else {
111- args .put (key , val );
112- }
113- i ++;
114- continue ;
115- }
116- String key = normalizeKey (t .substring (2 ));
117- if (isFormatKey (key )) {
118- if (i + 1 >= adapterAndRest .size ()) {
119- throw new IllegalArgumentException ("--" + key + " requires a value" );
120- }
121- format = adapterAndRest .get (i + 1 ).trim ();
122- i += 2 ;
123- continue ;
124- }
125- String next = i + 1 < adapterAndRest .size () ? adapterAndRest .get (i + 1 ) : null ;
126- if (Objects .nonNull (next ) && !next .trim ().startsWith ("-" )) {
127- args .put (key , next .trim ());
128- i += 2 ;
129- } else {
130- args .put (key , Boolean .TRUE );
131- i ++;
61+ int separator = token .indexOf ('=' );
62+ String option = separator < 0 ? token : token .substring (0 , separator );
63+ boolean shortFormat = "-f" .equals (option ) && separator < 0 ;
64+ if (!shortFormat && !option .startsWith ("--" )) {
65+ throw new IllegalArgumentException ("Legacy collect does not support this option syntax" );
66+ }
67+ String key = shortFormat ? "format" : requireIdentifier (option .substring (2 ));
68+ boolean isFormat = "format" .equals (key ) || "f" .equals (key );
69+ Object value = Boolean .TRUE ;
70+ if (separator >= 0 ) {
71+ value = requireValue (token .substring (separator + 1 ));
72+ } else if (index < adapterAndRest .size ()
73+ && (Objects .isNull (adapterAndRest .get (index )) || !adapterAndRest .get (index ).startsWith ("-" ))) {
74+ value = requireValue (adapterAndRest .get (index ++));
75+ }
76+ if (isFormat ) {
77+ if (formatSpecified || !(value instanceof String )) {
78+ throw new IllegalArgumentException ("Legacy collect requires a single explicit format value" );
13279 }
133- continue ;
80+ format = (String ) value ;
81+ formatSpecified = true ;
82+ } else if (Objects .nonNull (args .putIfAbsent (key , value ))) {
83+ throw new IllegalArgumentException ("Legacy collect cannot preserve a repeated option" );
13484 }
135-
136- positional .add (t );
137- i ++;
13885 }
13986
14087 OpenCliCollectRequest .OpenCliCollectRequestBuilder b =
@@ -149,14 +96,17 @@ public static OpenCliCollectRequest parse(
14996 return request ;
15097 }
15198
152- private static String normalizeKey (String key ) {
153- if (Objects .isNull (key ) || key . isEmpty ()) {
154- throw new IllegalArgumentException ("empty option key " );
99+ private static String requireIdentifier (String value ) {
100+ if (Objects .isNull (value ) || ! IDENTIFIER . matcher ( value ). matches ()) {
101+ throw new IllegalArgumentException ("Legacy collect requires a valid adapter, command or option identifier " );
155102 }
156- return key . toLowerCase ( Locale . ROOT ) ;
103+ return value ;
157104 }
158105
159- private static boolean isFormatKey (String key ) {
160- return "format" .equals (key ) || "f" .equals (key );
106+ private static String requireValue (String value ) {
107+ if (Objects .isNull (value ) || value .isEmpty () || !value .equals (value .trim ()) || value .startsWith ("-" )) {
108+ throw new IllegalArgumentException ("Legacy collect cannot preserve this option value" );
109+ }
110+ return value ;
161111 }
162112}
0 commit comments