Parses and validates compilation arguments.
| 109 | |
| 110 | |
| 111 | class CompilationArgumentParser: |
| 112 | """Parses and validates compilation arguments.""" |
| 113 | |
| 114 | def __init__(self, project_root: Path): |
| 115 | self.project_root = project_root |
| 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.""" |
| 158 | parser = argparse.ArgumentParser( |
| 159 | description="Compile FastLED examples for various boards using PioCompiler" |
| 160 | ) |
| 161 | |
| 162 | # Positional arguments |
| 163 | parser.add_argument( |
| 164 | "boards", |
| 165 | type=str, |
| 166 | help="Comma-separated list of boards to compile for", |
| 167 | nargs="?", |
| 168 | ) |