Skip to content

Commit 130066d

Browse files
authored
refactor(compiler): refactor fory compiler command line (#3243)
## Why? ## What does this PR do? refactor fory compiler command line: rename `fory compile` to `foryc` ## Related issues #3099 ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark
1 parent 8f69605 commit 130066d

7 files changed

Lines changed: 89 additions & 81 deletions

File tree

AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,8 @@ Fory uses binary protocols for efficient serialization and deserialization. Fory
413413
- **Location**: `compiler/` contains the Fory compiler, parser, IR, and code generators.
414414
- **Install & CLI**:
415415
- `cd compiler && pip install -e .`
416-
- `fory compile --help`
417-
- `fory compile schema.fdl --lang <langs> --output <dir>`
416+
- `foryc --help`
417+
- `foryc schema.fdl --lang <langs> --output <dir>`
418418
- **Generated code**: Never edit generated files manually; update the `.fdl`/`.proto`/`.fbs` and re-run the compiler.
419419
- **IDL tests**: Use `integration_tests/idl_tests/generate_idl.py` for test codegen.
420420
- In `integration_tests/idl_tests`, keep package names aligned with the IDL filename.

compiler/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ message Cat [id=103] {
6161

6262
```bash
6363
# Generate for all languages
64-
fory compile schema.fdl --output ./generated
64+
foryc schema.fdl --output ./generated
6565

6666
# Generate for specific languages
67-
fory compile schema.fdl --lang java,python --output ./generated
67+
foryc schema.fdl --lang java,python --output ./generated
6868

6969
# Override package name
70-
fory compile schema.fdl --package myapp.models --output ./generated
70+
foryc schema.fdl --package myapp.models --output ./generated
7171

7272
# Language-specific output directories (protoc-style)
73-
fory compile schema.fdl --java_out=./src/main/java --python_out=./python/src
73+
foryc schema.fdl --java_out=./src/main/java --python_out=./python/src
7474

7575
# Combine with other options
76-
fory compile schema.fdl --java_out=./gen --go_out=./gen/go -I ./proto
76+
foryc schema.fdl --java_out=./gen --go_out=./gen/go -I ./proto
7777
```
7878

7979
### 3. Use Generated Code
@@ -394,7 +394,7 @@ struct Cat {
394394
## CLI Reference
395395
396396
```
397-
fory compile [OPTIONS] FILES...
397+
foryc [OPTIONS] FILES...
398398

399399
Arguments:
400400
FILES FDL files to compile
@@ -414,7 +414,7 @@ See the `examples/` directory for sample FDL files and generated output.
414414
415415
```bash
416416
# Compile the demo schema
417-
fory compile examples/demo.fdl --output examples/generated
417+
foryc examples/demo.fdl --output examples/generated
418418
```
419419

420420
## Development
@@ -427,7 +427,7 @@ pip install -e .
427427
python -m fory_compiler compile examples/demo.fdl
428428

429429
# Or use the installed command
430-
fory compile examples/demo.fdl
430+
foryc examples/demo.fdl
431431
```
432432

433433
## License

compiler/fory_compiler/cli.py

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -207,49 +207,47 @@ def resolve_go_output_dir(base_go_out: Path, schema: Schema) -> Path:
207207
def parse_args(args: Optional[List[str]] = None) -> argparse.Namespace:
208208
"""Parse command line arguments."""
209209
parser = argparse.ArgumentParser(
210-
prog="fory",
210+
prog="foryc",
211211
description="Fory IDL compiler",
212212
)
213213

214-
subparsers = parser.add_subparsers(dest="command", help="Available commands")
215-
216-
# compile command
217-
compile_parser = subparsers.add_parser(
218-
"compile",
219-
help="Compile IDL files (.fdl, .proto, .fbs) to language-specific code",
214+
parser.add_argument(
215+
"--scan-generated",
216+
action="store_true",
217+
help="Scan for generated files under the current directory",
220218
)
221219

222-
compile_parser.add_argument(
220+
parser.add_argument(
223221
"files",
224-
nargs="+",
222+
nargs="*",
225223
type=Path,
226224
metavar="FILE",
227225
help="IDL files to compile",
228226
)
229227

230-
compile_parser.add_argument(
228+
parser.add_argument(
231229
"--lang",
232230
type=str,
233231
default="all",
234232
help="Comma-separated list of target languages (java,python,cpp,rust,go). Default: all",
235233
)
236234

237-
compile_parser.add_argument(
235+
parser.add_argument(
238236
"--output",
239237
"-o",
240238
type=Path,
241239
default=Path("./generated"),
242240
help="Output directory. Default: ./generated",
243241
)
244242

245-
compile_parser.add_argument(
243+
parser.add_argument(
246244
"--package",
247245
type=str,
248246
default=None,
249247
help="Override package name from FDL file",
250248
)
251249

252-
compile_parser.add_argument(
250+
parser.add_argument(
253251
"-I",
254252
"--proto_path",
255253
"--import_path",
@@ -262,83 +260,79 @@ def parse_args(args: Optional[List[str]] = None) -> argparse.Namespace:
262260
)
263261

264262
# Language-specific output directories (protoc-style)
265-
compile_parser.add_argument(
263+
parser.add_argument(
266264
"--java_out",
267265
type=Path,
268266
default=None,
269267
metavar="DST_DIR",
270268
help="Generate Java code in DST_DIR",
271269
)
272270

273-
compile_parser.add_argument(
271+
parser.add_argument(
274272
"--python_out",
275273
type=Path,
276274
default=None,
277275
metavar="DST_DIR",
278276
help="Generate Python code in DST_DIR",
279277
)
280278

281-
compile_parser.add_argument(
279+
parser.add_argument(
282280
"--cpp_out",
283281
type=Path,
284282
default=None,
285283
metavar="DST_DIR",
286284
help="Generate C++ code in DST_DIR",
287285
)
288286

289-
compile_parser.add_argument(
287+
parser.add_argument(
290288
"--go_out",
291289
type=Path,
292290
default=None,
293291
metavar="DST_DIR",
294292
help="Generate Go code in DST_DIR",
295293
)
296294

297-
compile_parser.add_argument(
295+
parser.add_argument(
298296
"--rust_out",
299297
type=Path,
300298
default=None,
301299
metavar="DST_DIR",
302300
help="Generate Rust code in DST_DIR",
303301
)
304302

305-
compile_parser.add_argument(
303+
parser.add_argument(
306304
"--go_nested_type_style",
307305
type=str,
308306
default=None,
309307
choices=["camelcase", "underscore"],
310308
help="Go nested type naming style: camelcase or underscore (default)",
311309
)
312310

313-
compile_parser.add_argument(
311+
parser.add_argument(
314312
"--emit-fdl",
315313
action="store_true",
316314
help="Emit translated FDL (for non-FDL inputs) for debugging",
317315
)
318316

319-
compile_parser.add_argument(
317+
parser.add_argument(
320318
"--emit-fdl-path",
321319
type=Path,
322320
default=None,
323321
help="Write translated FDL to this path (file or directory)",
324322
)
325323

326-
scan_parser = subparsers.add_parser(
327-
"scan-generated",
328-
help="Scan for generated files under the current directory",
329-
)
330-
scan_parser.add_argument(
324+
parser.add_argument(
331325
"--root",
332326
type=Path,
333327
default=Path("."),
334328
help="Root directory to scan (default: current directory)",
335329
)
336-
scan_parser.add_argument(
330+
parser.add_argument(
337331
"--relative",
338332
action="store_true",
339333
help="Print paths relative to the scan root",
340334
)
341-
delete_group = scan_parser.add_mutually_exclusive_group()
335+
delete_group = parser.add_mutually_exclusive_group()
342336
delete_group.add_argument(
343337
"--delete",
344338
action="store_true",
@@ -353,6 +347,23 @@ def parse_args(args: Optional[List[str]] = None) -> argparse.Namespace:
353347
return parser.parse_args(args)
354348

355349

350+
def normalize_args(args: Optional[List[str]]) -> List[str]:
351+
"""Normalize args so compile is the default command."""
352+
if args is None:
353+
args = sys.argv[1:]
354+
if not args:
355+
return []
356+
if args[0] in {"-h", "--help"}:
357+
return args
358+
if args[0] == "--scan-generated":
359+
return args
360+
if args[0] == "scan-generated":
361+
return ["--scan-generated", *args[1:]]
362+
if args[0] == "compile":
363+
return args[1:]
364+
return args
365+
366+
356367
def get_languages(lang_arg: str) -> List[str]:
357368
"""Parse the language argument into a list of languages."""
358369
if lang_arg == "all":
@@ -656,20 +667,17 @@ def cmd_scan_generated(args: argparse.Namespace) -> int:
656667

657668
def main(args: Optional[List[str]] = None) -> int:
658669
"""Main entry point."""
659-
parsed = parse_args(args)
660-
661-
if parsed.command is None:
662-
print("Usage: fory <command> [options]", file=sys.stderr)
663-
print("Commands: compile, scan-generated", file=sys.stderr)
664-
print("Use 'fory <command> --help' for more information", file=sys.stderr)
665-
return 1
670+
parsed = parse_args(normalize_args(args))
666671

667-
if parsed.command == "compile":
668-
return cmd_compile(parsed)
669-
if parsed.command == "scan-generated":
672+
if parsed.scan_generated:
670673
return cmd_scan_generated(parsed)
671674

672-
return 0
675+
if not parsed.files:
676+
print("Usage: foryc [--scan-generated] [options] FILES...", file=sys.stderr)
677+
print("Use 'foryc --help' for more information", file=sys.stderr)
678+
return 1
679+
680+
return cmd_compile(parsed)
673681

674682

675683
if __name__ == "__main__":

compiler/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ classifiers = [
4242
keywords = ["fory", "serialization", "codegen", "idl", "compiler"]
4343

4444
[project.scripts]
45-
fory = "fory_compiler.cli:main"
45+
foryc = "fory_compiler.cli:main"
4646

4747
[project.urls]
4848
Homepage = "https://github.com/apache/fory"

0 commit comments

Comments
 (0)