|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +from os.path import dirname, join |
| 17 | + |
| 18 | +import click |
| 19 | + |
| 20 | + |
| 21 | +class PlatformioCLI(click.MultiCommand): |
| 22 | + |
| 23 | + leftover_args = [] |
| 24 | + |
| 25 | + def invoke(self, ctx): |
| 26 | + PlatformioCLI.leftover_args = ctx.args |
| 27 | + if hasattr(ctx, "protected_args"): |
| 28 | + PlatformioCLI.leftover_args = ctx.protected_args + ctx.args |
| 29 | + return super(PlatformioCLI, self).invoke(ctx) |
| 30 | + |
| 31 | + def list_commands(self, ctx): |
| 32 | + cmds = [] |
| 33 | + for filename in os.listdir(join(dirname(__file__), "commands")): |
| 34 | + if filename.startswith("__init__"): |
| 35 | + continue |
| 36 | + if filename.endswith(".py"): |
| 37 | + cmds.append(filename[:-3]) |
| 38 | + cmds.sort() |
| 39 | + return cmds |
| 40 | + |
| 41 | + def get_command(self, ctx, cmd_name): |
| 42 | + mod = None |
| 43 | + try: |
| 44 | + mod = __import__("platformio.commands." + cmd_name, None, None, |
| 45 | + ["cli"]) |
| 46 | + except ImportError: |
| 47 | + try: |
| 48 | + return self._handle_obsolate_command(cmd_name) |
| 49 | + except AttributeError: |
| 50 | + raise click.UsageError('No such command "%s"' % cmd_name, ctx) |
| 51 | + return mod.cli |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def _handle_obsolate_command(name): |
| 55 | + if name == "platforms": |
| 56 | + from platformio.commands import platform |
| 57 | + return platform.cli |
| 58 | + if name == "serialports": |
| 59 | + from platformio.commands import device |
| 60 | + return device.cli |
| 61 | + raise AttributeError() |
0 commit comments