Added support for the latest Python "Click" package (CLI Builder) // Resolve #349

This commit is contained in:
Ivan Kravets
2019-05-09 00:51:28 +03:00
parent 7687a0a929
commit 62b80c396b
11 changed files with 75 additions and 58 deletions

View File

@@ -11,3 +11,51 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from os.path import dirname, join
import click
class PlatformioCLI(click.MultiCommand):
leftover_args = []
def invoke(self, ctx):
PlatformioCLI.leftover_args = ctx.args
if hasattr(ctx, "protected_args"):
PlatformioCLI.leftover_args = ctx.protected_args + ctx.args
return super(PlatformioCLI, self).invoke(ctx)
def list_commands(self, ctx):
cmds = []
for filename in os.listdir(join(dirname(__file__), "commands")):
if filename.startswith("__init__"):
continue
if filename.endswith(".py"):
cmds.append(filename[:-3])
cmds.sort()
return cmds
def get_command(self, ctx, cmd_name):
mod = None
try:
mod = __import__("platformio.commands." + cmd_name, None, None,
["cli"])
except ImportError:
try:
return self._handle_obsolate_command(cmd_name)
except AttributeError:
raise click.UsageError('No such command "%s"' % cmd_name, ctx)
return mod.cli
@staticmethod
def _handle_obsolate_command(name):
if name == "platforms":
from platformio.commands import platform
return platform.cli
if name == "serialports":
from platformio.commands import device
return device.cli
raise AttributeError()

View File

@@ -21,6 +21,7 @@ from os.path import isdir, join
import click
from platformio import exception, util
from platformio.commands import PlatformioCLI
from platformio.managers.lib import LibraryManager, get_builtin_libs
from platformio.project.helpers import (
get_project_dir, get_projectlibdeps_dir, is_platformio_project)
@@ -78,7 +79,7 @@ def cli(ctx, **options):
ctx.invoked_subcommand)
ctx.obj = LibraryManager(storage_dir)
if "--json-output" not in ctx.args:
if "--json-output" not in PlatformioCLI.leftover_args:
click.echo("Library Storage: " + storage_dir)