likeshell


Namelikeshell JSON
Version 1.1.3 PyPI version JSON
download
home_pagehttps://github.com/Orisdaddy/likeshell
Summary快速构建一个干净整洁的命令行工具包。Quickly develop your command line interface(CLI) toolkit.
upload_time2021-05-28 08:42:44
maintainer
docs_urlNone
authorFizone
requires_python>=3.6
licenseApache License 2.0
keywords shell command line cli
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # likeshell

[![PyPI version](https://badge.fury.io/py/likeshell.svg)](https://badge.fury.io/py/likeshell)
[![Supported Versions](https://img.shields.io/pypi/pyversions/likeshell.svg)](https://pypi.org/project/likeshell)
[![Apache License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)

[![Actions Status](https://github.com/Orisdaddy/likeshell/workflows/Macos%20Test/badge.svg)](https://github.com/Orisdaddy/likeshell/actions)
[![codecov](https://codecov.io/gh/Orisdaddy/likeshell/branch/master/graph/badge.svg)](https://codecov.io/gh/Orisdaddy/likeshell)


likeshell 快速构建自己的命令行工具,扩展性强、可配置、开箱即用、干净整洁,只需一步构建自己的CLI。

likeshell通过定义类中方法的形式定义CLI中的命令,极少有任务逻辑外的代码,使用简单,使开发的CLI程序简洁易用可读性强。


## 要求

Python >= 3.6

## 安装

使用pip安装

```shell script
$ pip install likeshell
```


## 简单使用

1.创建一个python文件,这里命名为demo.py。

```python
import likeshell

class MyTasks(likeshell.Shell):  # 定义类并继承likeshell.Shell,类名并不影响程序。
    def task1(        # 命令名默认为方法名称
            self,     # 默认按顺序输入参数,self被忽略
            s1,       # 不指定类型则不校验
            i1: int,  # 指定类型会校验参数类型
            f1: float
        ):
        print('run test1')
        print(f's1 is {s1}')
        print(f'i1 is {i1}')
        print(f'f1 is {f1}')

    def task2(self):
        print('run test2')
```

2.在终端中输入。

```shell script
>> python demo.py task1 str1 100 10.01
<< run test1
   s1 is str1
   i1 is 100
   f1 is 10.01

>> python demo.py task1
<< likeshell.exceptions.ParameterError: MissingParameter: s1.

>> python demo.py task1 str1 str2 str3
<< likeshell.exceptions.ParameterError: ParameterTypeError: "str2" is not a int

>> python demo.py task2
<< run test2
```


## 功能文档
### 基础功能

- [基本使用](#基本使用)
- [参数](#参数)
- [钩子](#钩子)
- [帮助](#帮助)

### 高级功能

- [任务别名](#任务别名)
- [无视任务](#无视任务)
- [输入(input)](#输入)
- [可选参数](#可选参数)
- [命令执行](#命令执行)
- [程序入口](#程序入口)
- [配置](#配置)



## 基本使用

基础使用可完全使用Python语法,除继承操作外不需要使用`likeshell`中的任何方法。

1.创建命令类并继承`likeshell.Shell`
2.创建相应的命令任务(命令默认为方法名)
3.定义参数

```python
# 导入likeshell包
import likeshell

# 创建类并继承likeshell.Shell
class MyTasks(likeshell.Shell):
    # 创建相应的命令任务
    def task1(self, s1, i1, f1):
        print('run test1')
        print(f's1 is {s1}')
        print(f'i1 is {i1}')
        print(f'f1 is {f1}')

    def task2(self):
        print('run test2')
```

## 参数

任务中的参数定义

### 参数类型

通过type annotation语法增加类型校验

```python
import likeshell

class MyTasks(likeshell.Shell):
    def task1(self, s1):
        print(f's1 is {s1}')

    def task2(self, i1: int):
        print(f'i1 is {i1}')
```

```shell script
>> python demo.py task1 100
<< s1 is 100

>> python demo.py task2 str1
<< likeshell.exceptions.ParameterError: ParameterTypeError: "str1" is not a int
```

### 可变长参数(*args)

```python
import likeshell

class MyTasks(likeshell.Shell):
    def task1(self, *args):
        print(f'args: {args}')
```

```shell script
>> python demo.py task1 a1 a2 a3
<< args: ['a1', 'a2', 'a3']
```

### 非必要参数与默认值

```python
import likeshell

class MyTasks(likeshell.Shell):
    def task1(
            self,
            s1: str,
            *,  # 通过*分割必要参数与非必要参数
            s2: str = 'string'
        ):
        print(f's1: {s1}')
        print(f's2: {s2}')
```

```shell script
>> python demo.py task1 str1
<< s1: str1
   s2: string

>> python demo.py task1 str1 str2
<< s1: str1
   s2: str2
```

## 钩子

```python
import likeshell

class GitShell(likeshell.Shell):
    # 在命令执行之前生效
    def __before__(self):
        print('run before')

    # 在命令执行之后生效
    def __after__(self):
        print('run after')
```

## 帮助

通过`-h`或`--help`获得方法相关的提示

```python
import likeshell

class MyTasks(likeshell.Shell):
    @likeshell.desc('This is task1')  # likeshell.desc定义命令的简要说明 在-h中打印 不定义默认打印comment多行注释中的有效内容第一行
    def task1(self, s1, i1, f1):
        """
        定义多行注释,help会将其作为命令说明打印
        task1说明:
            task1 takes 3 arguments
        """
        print('run test1')
        print(f's1 is {s1}')
        print(f'i1 is {i1}')
        print(f'f1 is {f1}')

    def task2(self):
        """
        task is a method
        """
        print('run test2')
```

```shell script
>> python demo.py
<< 帮助:
      <shell> -h
      <shell> -h <action>
   用法:
      <shell> <action> [options...]

   命令:
      task1             This is task1
      task2             task is a method


>> python demo.py -h task1
<< 定义多行注释,help会将其作为命令说明打印
   task1说明:
       task1 takes 3 arguments
```

## 任务别名

在程序中命令默认去匹配方法名,
当希望使用的命令名为python中关键字、包含特殊字符或希望定义的更加简短时,可以通过定义别名的方式实现。

likeshell提供两种方式来实现。

### 通过装饰器

使用`likeshell.alias`装饰器实现别名定义

```python
import likeshell

class MyTasks(likeshell.Shell):
    # 定义方法task1的别名为lambda
    @likeshell.alias('lambda')
    def task1(self):
        print('run task1')
```

```shell script
>> python demo.py lambda
<< run task1
```

### 通过多行注释
在方法中定义`:alias`开头的多行注释 
通过空格` `或`:`分割实现别名定义

```python
import likeshell

class MyTasks(likeshell.Shell):
    def task1(self):
        """
        定义方法task1的别名为lambda

        :alias: lambda
        """
        print('run task1')
```

```shell script
>> python demo.py lambda
<< run task1
```

## 无视任务

让指定任务命令不再被识别,也无法再调用

### 通过装饰器

使用`likeshell.ignore`装饰器无视指定的方法

```python
import likeshell

class MyTasks(likeshell.Shell):
    @likeshell.ignore
    def task1(self):
        print('run task1')
```

```shell script
>> python demo.py task1
<< RuntimeError: task1 is not found.
```

## 输入

实现用户输入

### 使用

```python
import likeshell


class MyTasks(likeshell.Shell):
    def task1(
            self,
            a: likeshell.Input  # 定义类型为 likeshell.Input
        ):
        print('run task1')
        print(a)
```

```shell script
>> python demo.py task1
>> a:
>> a: hello world
<< run task1
   hello world
```

### 参数说明

```python
import likeshell


def valid(arg):
    arg += '!'
    return arg


class MyTasks(likeshell.Shell):
    def task1(
            self,
            a: str,
            b: likeshell.Input(prompt='username:', default='default', hide=False, callback=valid),
            c: str
    ):
        """
        参数说明
            :prompt: 输入时显示的提示字符串
            :default: 输入为空时 使用默认值 也可以通过定义参数默认值实现
            :hide: 隐藏输入内容 默认为False
            :callback: 回调函数 要求接收一个参数 返回一个参数
        """
        print('run task1')
        print(a)
        print(b)
        print(c)
```

```shell script
>> python demo.py task1 astr cstr
>> username: joker
<< run task1
   astr
   joker!
   cstr
```

## 可选参数

支持以`-a`、`--arg`绑定标签的形式定义参数


### 通过类型定义

```python
import likeshell


class MyTasks(likeshell.Shell):
    def task1(
            self,
            a1,
            a2: likeshell.Options(tag='--a2', arglen=2),
    ):
        """
        参数说明
            :tag: 可识别的标签名 多个使用元组或列表例如['-a', '--arg']
            :arglen: 指定接收的参数个数
        """
        print(a1, a2[0], a2[1])
        assert a1 == 'hello'
        assert a2 == ['world', '!']
```

```shell script
>> python demo.py task1 hello --a2 world !
<< hello world !
```


### 通过装饰器

```python
import likeshell


class MyTasks(likeshell.Shell):
    @likeshell.Options(arg='a1', tag='--a1')
    @likeshell.Options(arg='a2', tag='--a2')
    def task1(
            self,
            a1,
            a2,
    ): 
        """
        参数说明
            :arg: 可选参数对应的位置参数名称
            :tag: 可识别的标签名 要定义多个标签则使用元组或列表 例如['-a', '--arg']
            :arglen: 指定接收的参数个数 
                     定义为0 将会接收到为'exist'的字符串或默认值
                     默认定义为1 接收指定的字符串或默认值
                     定义为大于1的数组 接收多个参数的列表或默认值
        """
        print(a1, a2)
        assert a1 == 'hello'
        assert a2 == 'world'
```


```shell script
>> python demo.py task1 --a2 world -a1 hello
<< hello world
```

## 命令执行

### 使用

```python
import likeshell


class MyTasks(likeshell.Shell):
    def task1(self):
        self.cmd('git branch')
        # likeshell.cmd('git branch')  # 两者效果一样

        # p = self.cmd('git branch', popen=True)  # 通过子进程执行命令
        # print(p.pid)
```

```shell script
>> python demo.py task1
>> * master
```

## 程序入口

当CLI程序需要配置入口时

```python
import likeshell
from likeshell.shell import run_cls


class MyTasks(likeshell.Main):  # 继承 likeshell.Main
    def task1(self):
        print('run task1')


def run():
    # 调用run_cls方法
    run_cls(MyTasks, MyTasks.__dict__)

```

## 配置

```python
import likeshell

class MyTasks(likeshell.Shell):
    __default_bash__ = 'git'  # 命令未击中时 可以使用其他cli (默认为None)
    __options_handler__ = likeshell.SimpleOptionsHandler()  # 参数处理器

    def task1(self):
        print('run task1')
```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Orisdaddy/likeshell",
    "name": "likeshell",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "shell,command,line,cli",
    "author": "Fizone",
    "author_email": "edeport126@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e9/8b/17f6593397a517ab4f46303fef7369d44a6ba825e96f5ca71f934f77051c/likeshell-1.1.3.tar.gz",
    "platform": "any",
    "description": "# likeshell\n\n[![PyPI version](https://badge.fury.io/py/likeshell.svg)](https://badge.fury.io/py/likeshell)\n[![Supported Versions](https://img.shields.io/pypi/pyversions/likeshell.svg)](https://pypi.org/project/likeshell)\n[![Apache License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)\n\n[![Actions Status](https://github.com/Orisdaddy/likeshell/workflows/Macos%20Test/badge.svg)](https://github.com/Orisdaddy/likeshell/actions)\n[![codecov](https://codecov.io/gh/Orisdaddy/likeshell/branch/master/graph/badge.svg)](https://codecov.io/gh/Orisdaddy/likeshell)\n\n\nlikeshell \u5feb\u901f\u6784\u5efa\u81ea\u5df1\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u6269\u5c55\u6027\u5f3a\u3001\u53ef\u914d\u7f6e\u3001\u5f00\u7bb1\u5373\u7528\u3001\u5e72\u51c0\u6574\u6d01\uff0c\u53ea\u9700\u4e00\u6b65\u6784\u5efa\u81ea\u5df1\u7684CLI\u3002\n\nlikeshell\u901a\u8fc7\u5b9a\u4e49\u7c7b\u4e2d\u65b9\u6cd5\u7684\u5f62\u5f0f\u5b9a\u4e49CLI\u4e2d\u7684\u547d\u4ee4\uff0c\u6781\u5c11\u6709\u4efb\u52a1\u903b\u8f91\u5916\u7684\u4ee3\u7801\uff0c\u4f7f\u7528\u7b80\u5355\uff0c\u4f7f\u5f00\u53d1\u7684CLI\u7a0b\u5e8f\u7b80\u6d01\u6613\u7528\u53ef\u8bfb\u6027\u5f3a\u3002\n\n\n## \u8981\u6c42\n\nPython >= 3.6\n\n## \u5b89\u88c5\n\n\u4f7f\u7528pip\u5b89\u88c5\n\n```shell script\n$ pip install likeshell\n```\n\n\n## \u7b80\u5355\u4f7f\u7528\n\n1.\u521b\u5efa\u4e00\u4e2apython\u6587\u4ef6\uff0c\u8fd9\u91cc\u547d\u540d\u4e3ademo.py\u3002\n\n```python\nimport likeshell\n\nclass MyTasks(likeshell.Shell):  # \u5b9a\u4e49\u7c7b\u5e76\u7ee7\u627flikeshell.Shell\uff0c\u7c7b\u540d\u5e76\u4e0d\u5f71\u54cd\u7a0b\u5e8f\u3002\n    def task1(        # \u547d\u4ee4\u540d\u9ed8\u8ba4\u4e3a\u65b9\u6cd5\u540d\u79f0\n            self,     # \u9ed8\u8ba4\u6309\u987a\u5e8f\u8f93\u5165\u53c2\u6570\uff0cself\u88ab\u5ffd\u7565\n            s1,       # \u4e0d\u6307\u5b9a\u7c7b\u578b\u5219\u4e0d\u6821\u9a8c\n            i1: int,  # \u6307\u5b9a\u7c7b\u578b\u4f1a\u6821\u9a8c\u53c2\u6570\u7c7b\u578b\n            f1: float\n        ):\n        print('run test1')\n        print(f's1 is {s1}')\n        print(f'i1 is {i1}')\n        print(f'f1 is {f1}')\n\n    def task2(self):\n        print('run test2')\n```\n\n2.\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u3002\n\n```shell script\n>> python demo.py task1 str1 100 10.01\n<< run test1\n   s1 is str1\n   i1 is 100\n   f1 is 10.01\n\n>> python demo.py task1\n<< likeshell.exceptions.ParameterError: MissingParameter: s1.\n\n>> python demo.py task1 str1 str2 str3\n<< likeshell.exceptions.ParameterError: ParameterTypeError: \"str2\" is not a int\n\n>> python demo.py task2\n<< run test2\n```\n\n\n## \u529f\u80fd\u6587\u6863\n### \u57fa\u7840\u529f\u80fd\n\n- [\u57fa\u672c\u4f7f\u7528](#\u57fa\u672c\u4f7f\u7528)\n- [\u53c2\u6570](#\u53c2\u6570)\n- [\u94a9\u5b50](#\u94a9\u5b50)\n- [\u5e2e\u52a9](#\u5e2e\u52a9)\n\n### \u9ad8\u7ea7\u529f\u80fd\n\n- [\u4efb\u52a1\u522b\u540d](#\u4efb\u52a1\u522b\u540d)\n- [\u65e0\u89c6\u4efb\u52a1](#\u65e0\u89c6\u4efb\u52a1)\n- [\u8f93\u5165(input)](#\u8f93\u5165)\n- [\u53ef\u9009\u53c2\u6570](#\u53ef\u9009\u53c2\u6570)\n- [\u547d\u4ee4\u6267\u884c](#\u547d\u4ee4\u6267\u884c)\n- [\u7a0b\u5e8f\u5165\u53e3](#\u7a0b\u5e8f\u5165\u53e3)\n- [\u914d\u7f6e](#\u914d\u7f6e)\n\n\n\n## \u57fa\u672c\u4f7f\u7528\n\n\u57fa\u7840\u4f7f\u7528\u53ef\u5b8c\u5168\u4f7f\u7528Python\u8bed\u6cd5\uff0c\u9664\u7ee7\u627f\u64cd\u4f5c\u5916\u4e0d\u9700\u8981\u4f7f\u7528`likeshell`\u4e2d\u7684\u4efb\u4f55\u65b9\u6cd5\u3002\n\n1.\u521b\u5efa\u547d\u4ee4\u7c7b\u5e76\u7ee7\u627f`likeshell.Shell`\n2.\u521b\u5efa\u76f8\u5e94\u7684\u547d\u4ee4\u4efb\u52a1(\u547d\u4ee4\u9ed8\u8ba4\u4e3a\u65b9\u6cd5\u540d)\n3.\u5b9a\u4e49\u53c2\u6570\n\n```python\n# \u5bfc\u5165likeshell\u5305\nimport likeshell\n\n# \u521b\u5efa\u7c7b\u5e76\u7ee7\u627flikeshell.Shell\nclass MyTasks(likeshell.Shell):\n    # \u521b\u5efa\u76f8\u5e94\u7684\u547d\u4ee4\u4efb\u52a1\n    def task1(self, s1, i1, f1):\n        print('run test1')\n        print(f's1 is {s1}')\n        print(f'i1 is {i1}')\n        print(f'f1 is {f1}')\n\n    def task2(self):\n        print('run test2')\n```\n\n## \u53c2\u6570\n\n\u4efb\u52a1\u4e2d\u7684\u53c2\u6570\u5b9a\u4e49\n\n### \u53c2\u6570\u7c7b\u578b\n\n\u901a\u8fc7type annotation\u8bed\u6cd5\u589e\u52a0\u7c7b\u578b\u6821\u9a8c\n\n```python\nimport likeshell\n\nclass MyTasks(likeshell.Shell):\n    def task1(self, s1):\n        print(f's1 is {s1}')\n\n    def task2(self, i1: int):\n        print(f'i1 is {i1}')\n```\n\n```shell script\n>> python demo.py task1 100\n<< s1 is 100\n\n>> python demo.py task2 str1\n<< likeshell.exceptions.ParameterError: ParameterTypeError: \"str1\" is not a int\n```\n\n### \u53ef\u53d8\u957f\u53c2\u6570(*args)\n\n```python\nimport likeshell\n\nclass MyTasks(likeshell.Shell):\n    def task1(self, *args):\n        print(f'args: {args}')\n```\n\n```shell script\n>> python demo.py task1 a1 a2 a3\n<< args: ['a1', 'a2', 'a3']\n```\n\n### \u975e\u5fc5\u8981\u53c2\u6570\u4e0e\u9ed8\u8ba4\u503c\n\n```python\nimport likeshell\n\nclass MyTasks(likeshell.Shell):\n    def task1(\n            self,\n            s1: str,\n            *,  # \u901a\u8fc7*\u5206\u5272\u5fc5\u8981\u53c2\u6570\u4e0e\u975e\u5fc5\u8981\u53c2\u6570\n            s2: str = 'string'\n        ):\n        print(f's1: {s1}')\n        print(f's2: {s2}')\n```\n\n```shell script\n>> python demo.py task1 str1\n<< s1: str1\n   s2: string\n\n>> python demo.py task1 str1 str2\n<< s1: str1\n   s2: str2\n```\n\n## \u94a9\u5b50\n\n```python\nimport likeshell\n\nclass GitShell(likeshell.Shell):\n    # \u5728\u547d\u4ee4\u6267\u884c\u4e4b\u524d\u751f\u6548\n    def __before__(self):\n        print('run before')\n\n    # \u5728\u547d\u4ee4\u6267\u884c\u4e4b\u540e\u751f\u6548\n    def __after__(self):\n        print('run after')\n```\n\n## \u5e2e\u52a9\n\n\u901a\u8fc7`-h`\u6216`--help`\u83b7\u5f97\u65b9\u6cd5\u76f8\u5173\u7684\u63d0\u793a\n\n```python\nimport likeshell\n\nclass MyTasks(likeshell.Shell):\n    @likeshell.desc('This is task1')  # likeshell.desc\u5b9a\u4e49\u547d\u4ee4\u7684\u7b80\u8981\u8bf4\u660e \u5728-h\u4e2d\u6253\u5370 \u4e0d\u5b9a\u4e49\u9ed8\u8ba4\u6253\u5370comment\u591a\u884c\u6ce8\u91ca\u4e2d\u7684\u6709\u6548\u5185\u5bb9\u7b2c\u4e00\u884c\n    def task1(self, s1, i1, f1):\n        \"\"\"\n        \u5b9a\u4e49\u591a\u884c\u6ce8\u91ca\uff0chelp\u4f1a\u5c06\u5176\u4f5c\u4e3a\u547d\u4ee4\u8bf4\u660e\u6253\u5370\n        task1\u8bf4\u660e:\n            task1 takes 3 arguments\n        \"\"\"\n        print('run test1')\n        print(f's1 is {s1}')\n        print(f'i1 is {i1}')\n        print(f'f1 is {f1}')\n\n    def task2(self):\n        \"\"\"\n        task is a method\n        \"\"\"\n        print('run test2')\n```\n\n```shell script\n>> python demo.py\n<< \u5e2e\u52a9:\n      <shell> -h\n      <shell> -h <action>\n   \u7528\u6cd5:\n      <shell> <action> [options...]\n\n   \u547d\u4ee4:\n      task1             This is task1\n      task2             task is a method\n\n\n>> python demo.py -h task1\n<< \u5b9a\u4e49\u591a\u884c\u6ce8\u91ca\uff0chelp\u4f1a\u5c06\u5176\u4f5c\u4e3a\u547d\u4ee4\u8bf4\u660e\u6253\u5370\n   task1\u8bf4\u660e:\n       task1 takes 3 arguments\n```\n\n## \u4efb\u52a1\u522b\u540d\n\n\u5728\u7a0b\u5e8f\u4e2d\u547d\u4ee4\u9ed8\u8ba4\u53bb\u5339\u914d\u65b9\u6cd5\u540d\uff0c\n\u5f53\u5e0c\u671b\u4f7f\u7528\u7684\u547d\u4ee4\u540d\u4e3apython\u4e2d\u5173\u952e\u5b57\u3001\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u6216\u5e0c\u671b\u5b9a\u4e49\u7684\u66f4\u52a0\u7b80\u77ed\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u522b\u540d\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002\n\nlikeshell\u63d0\u4f9b\u4e24\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\n\n### \u901a\u8fc7\u88c5\u9970\u5668\n\n\u4f7f\u7528`likeshell.alias`\u88c5\u9970\u5668\u5b9e\u73b0\u522b\u540d\u5b9a\u4e49\n\n```python\nimport likeshell\n\nclass MyTasks(likeshell.Shell):\n    # \u5b9a\u4e49\u65b9\u6cd5task1\u7684\u522b\u540d\u4e3alambda\n    @likeshell.alias('lambda')\n    def task1(self):\n        print('run task1')\n```\n\n```shell script\n>> python demo.py lambda\n<< run task1\n```\n\n### \u901a\u8fc7\u591a\u884c\u6ce8\u91ca\n\u5728\u65b9\u6cd5\u4e2d\u5b9a\u4e49`:alias`\u5f00\u5934\u7684\u591a\u884c\u6ce8\u91ca \n\u901a\u8fc7\u7a7a\u683c` `\u6216`:`\u5206\u5272\u5b9e\u73b0\u522b\u540d\u5b9a\u4e49\n\n```python\nimport likeshell\n\nclass MyTasks(likeshell.Shell):\n    def task1(self):\n        \"\"\"\n        \u5b9a\u4e49\u65b9\u6cd5task1\u7684\u522b\u540d\u4e3alambda\n\n        :alias: lambda\n        \"\"\"\n        print('run task1')\n```\n\n```shell script\n>> python demo.py lambda\n<< run task1\n```\n\n## \u65e0\u89c6\u4efb\u52a1\n\n\u8ba9\u6307\u5b9a\u4efb\u52a1\u547d\u4ee4\u4e0d\u518d\u88ab\u8bc6\u522b\uff0c\u4e5f\u65e0\u6cd5\u518d\u8c03\u7528\n\n### \u901a\u8fc7\u88c5\u9970\u5668\n\n\u4f7f\u7528`likeshell.ignore`\u88c5\u9970\u5668\u65e0\u89c6\u6307\u5b9a\u7684\u65b9\u6cd5\n\n```python\nimport likeshell\n\nclass MyTasks(likeshell.Shell):\n    @likeshell.ignore\n    def task1(self):\n        print('run task1')\n```\n\n```shell script\n>> python demo.py task1\n<< RuntimeError: task1 is not found.\n```\n\n## \u8f93\u5165\n\n\u5b9e\u73b0\u7528\u6237\u8f93\u5165\n\n### \u4f7f\u7528\n\n```python\nimport likeshell\n\n\nclass MyTasks(likeshell.Shell):\n    def task1(\n            self,\n            a: likeshell.Input  # \u5b9a\u4e49\u7c7b\u578b\u4e3a likeshell.Input\n        ):\n        print('run task1')\n        print(a)\n```\n\n```shell script\n>> python demo.py task1\n>> a:\n>> a: hello world\n<< run task1\n   hello world\n```\n\n### \u53c2\u6570\u8bf4\u660e\n\n```python\nimport likeshell\n\n\ndef valid(arg):\n    arg += '!'\n    return arg\n\n\nclass MyTasks(likeshell.Shell):\n    def task1(\n            self,\n            a: str,\n            b: likeshell.Input(prompt='username:', default='default', hide=False, callback=valid),\n            c: str\n    ):\n        \"\"\"\n        \u53c2\u6570\u8bf4\u660e\n            :prompt: \u8f93\u5165\u65f6\u663e\u793a\u7684\u63d0\u793a\u5b57\u7b26\u4e32\n            :default: \u8f93\u5165\u4e3a\u7a7a\u65f6 \u4f7f\u7528\u9ed8\u8ba4\u503c \u4e5f\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u53c2\u6570\u9ed8\u8ba4\u503c\u5b9e\u73b0\n            :hide: \u9690\u85cf\u8f93\u5165\u5185\u5bb9 \u9ed8\u8ba4\u4e3aFalse\n            :callback: \u56de\u8c03\u51fd\u6570 \u8981\u6c42\u63a5\u6536\u4e00\u4e2a\u53c2\u6570 \u8fd4\u56de\u4e00\u4e2a\u53c2\u6570\n        \"\"\"\n        print('run task1')\n        print(a)\n        print(b)\n        print(c)\n```\n\n```shell script\n>> python demo.py task1 astr cstr\n>> username: joker\n<< run task1\n   astr\n   joker!\n   cstr\n```\n\n## \u53ef\u9009\u53c2\u6570\n\n\u652f\u6301\u4ee5`-a`\u3001`--arg`\u7ed1\u5b9a\u6807\u7b7e\u7684\u5f62\u5f0f\u5b9a\u4e49\u53c2\u6570\n\n\n### \u901a\u8fc7\u7c7b\u578b\u5b9a\u4e49\n\n```python\nimport likeshell\n\n\nclass MyTasks(likeshell.Shell):\n    def task1(\n            self,\n            a1,\n            a2: likeshell.Options(tag='--a2', arglen=2),\n    ):\n        \"\"\"\n        \u53c2\u6570\u8bf4\u660e\n            :tag: \u53ef\u8bc6\u522b\u7684\u6807\u7b7e\u540d \u591a\u4e2a\u4f7f\u7528\u5143\u7ec4\u6216\u5217\u8868\u4f8b\u5982['-a', '--arg']\n            :arglen: \u6307\u5b9a\u63a5\u6536\u7684\u53c2\u6570\u4e2a\u6570\n        \"\"\"\n        print(a1, a2[0], a2[1])\n        assert a1 == 'hello'\n        assert a2 == ['world', '!']\n```\n\n```shell script\n>> python demo.py task1 hello --a2 world !\n<< hello world !\n```\n\n\n### \u901a\u8fc7\u88c5\u9970\u5668\n\n```python\nimport likeshell\n\n\nclass MyTasks(likeshell.Shell):\n    @likeshell.Options(arg='a1', tag='--a1')\n    @likeshell.Options(arg='a2', tag='--a2')\n    def task1(\n            self,\n            a1,\n            a2,\n    ): \n        \"\"\"\n        \u53c2\u6570\u8bf4\u660e\n            :arg: \u53ef\u9009\u53c2\u6570\u5bf9\u5e94\u7684\u4f4d\u7f6e\u53c2\u6570\u540d\u79f0\n            :tag: \u53ef\u8bc6\u522b\u7684\u6807\u7b7e\u540d \u8981\u5b9a\u4e49\u591a\u4e2a\u6807\u7b7e\u5219\u4f7f\u7528\u5143\u7ec4\u6216\u5217\u8868 \u4f8b\u5982['-a', '--arg']\n            :arglen: \u6307\u5b9a\u63a5\u6536\u7684\u53c2\u6570\u4e2a\u6570 \n                     \u5b9a\u4e49\u4e3a0 \u5c06\u4f1a\u63a5\u6536\u5230\u4e3a'exist'\u7684\u5b57\u7b26\u4e32\u6216\u9ed8\u8ba4\u503c\n                     \u9ed8\u8ba4\u5b9a\u4e49\u4e3a1 \u63a5\u6536\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u6216\u9ed8\u8ba4\u503c\n                     \u5b9a\u4e49\u4e3a\u5927\u4e8e1\u7684\u6570\u7ec4 \u63a5\u6536\u591a\u4e2a\u53c2\u6570\u7684\u5217\u8868\u6216\u9ed8\u8ba4\u503c\n        \"\"\"\n        print(a1, a2)\n        assert a1 == 'hello'\n        assert a2 == 'world'\n```\n\n\n```shell script\n>> python demo.py task1 --a2 world -a1 hello\n<< hello world\n```\n\n## \u547d\u4ee4\u6267\u884c\n\n### \u4f7f\u7528\n\n```python\nimport likeshell\n\n\nclass MyTasks(likeshell.Shell):\n    def task1(self):\n        self.cmd('git branch')\n        # likeshell.cmd('git branch')  # \u4e24\u8005\u6548\u679c\u4e00\u6837\n\n        # p = self.cmd('git branch', popen=True)  # \u901a\u8fc7\u5b50\u8fdb\u7a0b\u6267\u884c\u547d\u4ee4\n        # print(p.pid)\n```\n\n```shell script\n>> python demo.py task1\n>> * master\n```\n\n## \u7a0b\u5e8f\u5165\u53e3\n\n\u5f53CLI\u7a0b\u5e8f\u9700\u8981\u914d\u7f6e\u5165\u53e3\u65f6\n\n```python\nimport likeshell\nfrom likeshell.shell import run_cls\n\n\nclass MyTasks(likeshell.Main):  # \u7ee7\u627f likeshell.Main\n    def task1(self):\n        print('run task1')\n\n\ndef run():\n    # \u8c03\u7528run_cls\u65b9\u6cd5\n    run_cls(MyTasks, MyTasks.__dict__)\n\n```\n\n## \u914d\u7f6e\n\n```python\nimport likeshell\n\nclass MyTasks(likeshell.Shell):\n    __default_bash__ = 'git'  # \u547d\u4ee4\u672a\u51fb\u4e2d\u65f6 \u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6cli (\u9ed8\u8ba4\u4e3aNone)\n    __options_handler__ = likeshell.SimpleOptionsHandler()  # \u53c2\u6570\u5904\u7406\u5668\n\n    def task1(self):\n        print('run task1')\n```\n\n\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "\u5feb\u901f\u6784\u5efa\u4e00\u4e2a\u5e72\u51c0\u6574\u6d01\u7684\u547d\u4ee4\u884c\u5de5\u5177\u5305\u3002Quickly develop your command line interface(CLI) toolkit.",
    "version": "1.1.3",
    "split_keywords": [
        "shell",
        "command",
        "line",
        "cli"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "452def8be0ced25052952de92954e3cb",
                "sha256": "ceff18c6e4db969bd74d3d8ffe22f47c4f32d841cd93445f0003bb904d9bdfba"
            },
            "downloads": -1,
            "filename": "likeshell-1.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "452def8be0ced25052952de92954e3cb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 22594,
            "upload_time": "2021-05-28T08:42:43",
            "upload_time_iso_8601": "2021-05-28T08:42:43.418118Z",
            "url": "https://files.pythonhosted.org/packages/c3/62/67f809832cc168316225d63e80c25fa7b714d2d1dc45b88c9e9ddea674a2/likeshell-1.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "061c0b5cb2037a36608d249b9fc9657a",
                "sha256": "7b28937b7dcc0c484049ea63870f1b09771f8dcc53025e340574112d80f9fd22"
            },
            "downloads": -1,
            "filename": "likeshell-1.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "061c0b5cb2037a36608d249b9fc9657a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 24281,
            "upload_time": "2021-05-28T08:42:44",
            "upload_time_iso_8601": "2021-05-28T08:42:44.990789Z",
            "url": "https://files.pythonhosted.org/packages/e9/8b/17f6593397a517ab4f46303fef7369d44a6ba825e96f5ca71f934f77051c/likeshell-1.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-05-28 08:42:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": null,
    "github_project": "Orisdaddy",
    "error": "Could not fetch GitHub repository",
    "lcname": "likeshell"
}
        
Elapsed time: 0.29728s