Parse arguments and return validated config.
(self, args: Optional[list[str]] = None)
| 116 | self.examples_dir = project_root / "examples" |
| 117 | |
| 118 | def parse(self, args: Optional[list[str]] = None) -> CompilationConfig: |
| 119 | """Parse arguments and return validated config.""" |
| 120 | parser = self._create_parser() |
| 121 | |
| 122 | # Parse arguments with intelligent unknown handling |
| 123 | try: |
| 124 | parsed = parser.parse_intermixed_args(args) |
| 125 | unknown: list[str] = [] |
| 126 | except SystemExit: |
| 127 | parsed, unknown = parser.parse_known_args(args) |
| 128 | |
| 129 | # Add unknown non-flag arguments as examples |
| 130 | if unknown: |
| 131 | extra_examples: list[str] = [ |
| 132 | arg for arg in unknown if not arg.startswith("-") |
| 133 | ] |
| 134 | if extra_examples: |
| 135 | if ( |
| 136 | not hasattr(parsed, "positional_examples") |
| 137 | or parsed.positional_examples is None |
| 138 | ): |
| 139 | parsed.positional_examples = [] |
| 140 | # Type ignore for argparse.Namespace attribute which is dynamically set |
| 141 | parsed.positional_examples.extend(extra_examples) # type: ignore[attr-defined] |
| 142 | |
| 143 | # Build configuration |
| 144 | config = self._build_config(parsed) |
| 145 | |
| 146 | # Validate |
| 147 | errors = config.validate() |
| 148 | if errors: |
| 149 | print("❌ Configuration validation failed:") |
| 150 | for error in errors: |
| 151 | print(f" - {error}") |
| 152 | sys.exit(1) |
| 153 | |
| 154 | return config |
| 155 | |
| 156 | def _create_parser(self) -> argparse.ArgumentParser: |
| 157 | """Create argument parser with all options.""" |
no test coverage detected