{"id":18898416,"url":"https://github.com/hychen/pkg-ucltip","last_synced_at":"2026-03-01T10:30:17.312Z","repository":{"id":2080436,"uuid":"3019832","full_name":"hychen/pkg-ucltip","owner":"hychen","description":"debian package source of ucltip","archived":false,"fork":false,"pushed_at":"2018-09-12T20:29:38.000Z","size":108,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-31T08:45:42.466Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hychen.png","metadata":{"files":{"readme":"README.txt","changelog":"ChangeLog.txt","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-12-20T14:04:27.000Z","updated_at":"2019-04-02T04:25:54.000Z","dependencies_parsed_at":"2022-08-19T16:50:20.267Z","dependency_job_id":null,"html_url":"https://github.com/hychen/pkg-ucltip","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hychen%2Fpkg-ucltip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hychen%2Fpkg-ucltip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hychen%2Fpkg-ucltip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hychen%2Fpkg-ucltip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hychen","download_url":"https://codeload.github.com/hychen/pkg-ucltip/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239879317,"owners_count":19712176,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T08:42:33.248Z","updated_at":"2026-03-01T10:30:15.937Z","avatar_url":"https://github.com/hychen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vim:syn=rst:\n========================================\nUCLTIP - Use command line tool in Python\n========================================\n\nThis library makes you use command line tool in Python more easier.\nThe original idea are from GitPython project `http://pypi.python.org/pypi/GitPython/`\n\nThe basic concept is to transform:\n\n1) command as a instance,\n2) command options as arguments or function/method keyword arguments.\n\nFeature:\n\n- Transform CLI Tool arguments to Python function/method arguments\n- Transform CLI Tool Boolean option style to Python function/method keyword arguments\n- Transform CLI Tool Key-Value option style to Python function/method keyword arguments\n- Transform CLI Tool Key-Value option style to Python function/method keyword arguments\n- Transform CLI Command as Python Class and use it in your script\n- Set default options of Command for multiple use\n\n-------\nExample\n-------\n\n::\n\n\t\u003e\u003e\u003eexpr = ucltip.Cmd('expr')\n\t\u003e\u003e\u003eret = expr(3, '+', 4)\n\t\u003e\u003e\u003eret\n\t\"7\\n\"\n\ncreate a Cmd callable object\n\n::\n\t\u003e\u003e\u003eexpr = ucltip.Cmd('expr')\n\nrun Cmd object as a function to get result like executing `expr 3 + 4` in shell,\nthe result will be storeged in '''ret''' variable, as the following shows, you can think\ncommand line tool arguments as Python function arguments.\n\n::\n\t\u003e\u003e\u003eret = expr(3, '+', 4)\n\nif you want to only check what command string will be combined, you can doing dry run,\nbut ramind the command string will be split as a list.\n\n::\n\t\u003e\u003e\u003eexpr = ucltip.Cmd('expr')\n\t\u003e\u003e\u003eexpr.conf.dry_run = True\n\t\u003e\u003e\u003eexpr(3, \"+\", 4)\n\t['expr', '3', '+', '4']\n\n\nplease note that jhe command be executed by subprocess.call, it bypass the shell.\n\n::\n\n\t# the result is $HOME, and it will not show output directly\n\tprint Cmd('echo')(\"$HOME\")\n\nif you want execute command via shell and use shell enviroment variable, please\ndo as follow, if args of function includes '''via_shell=True''', the command be executed by os.system\n\n::\n\n\t# the result is \"/home/somebody\", and show output directly\n\tCmd('echo')(\"$HOME\", via_shell=True)\n\n-----------------------------------\nHandling Error of command execution\n-----------------------------------\nif the command you want to use is not exists, the exception ucltip.CommandNotFound raises\n\n::\n\n\t\u003e\u003e a=ucltip.Cmd('oo')\n\tTraceback (most recent call last):\n\t  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\n\t  File \"ucltip/__init__.py\", line 103, in __init__\n\t    raise CommandNotFound()\n\tucltip.CommandNotFound\n\nif the command be executed falied, the exception ucltip.CommandExecutedError raises\n\n::\n\n\t\u003e\u003e\u003e a=ucltip.Cmd('ls')\n\t\u003e\u003e\u003e a\n\tCmd object bound 'ls'\n\t\u003e\u003e\u003e a(ccc=True)\n\tTraceback (most recent call last):\n\t  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\n\t  File \"ucltip/__init__.py\", line 109, in __call__\n\t    return self._callProcess(*args, **kwargs)\n\t  File \"ucltip/__init__.py\", line 126, in _callProcess\n\t    return self.execute(call, **_kwargs)\n\t  File \"ucltip/__init__.py\", line 169, in execute\n\t    raise CommandExecutedError(status, stderr_value)\n\tucltip.CommandExecutedError: ls: unrecognized option '--ccc'\n\tTry `ls --help' for more information.\n\nhere is a example to hanlde error:\n\n::\n\n\ttry:\n\t\tprint ucltip.Cmd('ls')\n\texcept ucltip.CommandExecutedError as e:\n\t\tprint e\n\n--------------\nCommand Option\n--------------\n\nso far, we already leanr how to execute command with arguments, but how about command options?\nit is pretty easy also, just think command option like python keyword arguments.\n\n''Boolean option''\n\nwhen the type of keyword arguments's value is boolean, then this kind of keyword arguments\nwill be converted to command boolean option, for example, `-a` is equal '''func(a=True)'''\n\n::\n\t\u003e\u003e\u003els('/tmp', a=True)\n\t['ls', '/tmp', '-a']\n\nKey-Value option\n================\n\nwhen the type of keyword arguments's value is interge number or string, then these of keyword\narguments will be coverted to command key-value option, for example, '--quoting_style 1' is equal\n'''func(quoting_style=1)'''\n\n::\n\t\u003e\u003e\u003els('tmp', quoting_style='c')\n\t['ls', '--quoting-style', 'c']\n\nalso, you can change option style by set '''opt_style''' attribute, support option style are `gnu`,\n`posix`,`java`, the default value is `posix`.\n\nnote: java option style is not fully supported.\n\n::\n\t\u003e\u003e\u003els.conf.opt_style = 'gnu'\n\t\u003e\u003e\u003els('tmp', quoting_style='c')\n\t['ls', '--quoting-style=c']\n\nsome options is mutiple, which means the name is name, but you can give\nmany different values, for example\n\n::\n\t# `foo -a -b -o Dir=/var -o Dir::Cache=/tmp`\n\t# so you need to use make_optargs to create args if the opt name is duplicate\n\toptargs = ucltip.make_optargs('o', ('Dir=/var','Dir::Cache=/tmp'))\n\tCmd('foo')(optargs, a=True, b=True)\n\n-------------\nCmdDispatcher\n-------------\n\nThe CmdDispatcher is an object for mapping some command tools has sub command,\nlike `git`, `zenity`, `pbuilder`, `apt-get`, etc.\n\nmethod name indicates as sub command name, method arguements indicates sub command arguments,\nand method keyword arguments indicates sub command options.\n\n::\n\t\u003e\u003eapt_get = ucltip.CmdDispatcher('apt-get')\n\t\u003e\u003eapt_get.conf.dry_run = True\n\t\u003e\u003eapt_get.install('vim')\n\t['apt-get', 'install', 'vim']\n\nif sub command has prefix string, you can use '''subcmd_prefix''' attribute to set it.\n\n::\n\n\t\u003e\u003ezenity = ucltip.CmdDispatcher('zenity')\n\t\u003e\u003ezenity.subcmd_prefix = '--'\n\t\u003e\u003ezenity.conf.dry_run = True\n\t\u003e\u003ezenity.info(text='hello')\n\t['zenity', '--info', '--text hello']\n\n--------------\nDefault Option\n--------------\n\nthe options does not be stored after you execute command, if you want to keep options\nfor multiple using, you can use ''''opts''' function to set default options as the following\n\n::\n\n\t\u003e\u003e\u003els = ucltip.Cmd('ls)\n\t\u003e\u003e\u003els.conf.dry_run=True\n\t\u003e\u003e\u003els.opts(l=True)\n\t\u003e\u003e\u003els('/tmp')\n\t['ls', '/tmp', '-l']\n\nCmdDispatcher sub command will load default options from its parent, in this case,\n'''apt_get.install.opts()''' is the same as '''apt_get.opts()'''\n\n::\n\n\t\u003e\u003e\u003eapt_get = ucltip.CmdDispatcher('apt-get')\n\t\u003e\u003e\u003eapt_get.conf.dry_run = True\n\t\u003e\u003e\u003eapt_get.opts(t='maverick')\n\t\u003e\u003e\u003eapt_get.install.opts()\n\t{t':'maverick'}\n\t\u003e\u003e\u003eapt_get.install('vim')\n\tapt-get', 'install', 'vim', '-t maverick']\n\t\u003e\u003e\u003eapt_get.opts(t=False)\n\t\u003e\u003e\u003eapt_get.install('vim')\n\t['apt-get', 'install', 'vim']\n\nPipe\n====\nIn subprocess, the way for doing pipeline is\n\n::\n\n\t\u003e\u003e\u003ep1 = Popen([\"dmesg\"], stdout=PIPE)\n\t\u003e\u003e\u003ep2 = Popen([\"grep\", \"hda\"], stdin=p1.stdout, stdout=PIPE)\n\t\u003e\u003e\u003eoutput = p2.communicate()[0]\n\nwhich is not convenience when you want to pipe many commands.\n\nPipe class provide similar interface as C library `libpipeline` (http://libpipeline.nongnu.org/)\nthat manipulating pipelines of subprocesses in a flexible and convenient way in C.\n\nfirstly, you need to create a Pipe instance\n\n::\n\t\u003e\u003e\u003epipe = ucltip.Pipe()\n\nand then add some command arguments as you want\n\n::\n\n\t\u003e\u003e\u003epipe.add('expr', 1, '+' 3)\n\t\u003e\u003e\u003epipe.add('sed', 's/4/5/', '--posix')\n\nfinally run the process, wait it terminate and get its output\n\n::\n\n\t\u003e\u003e\u003epipe.wait()\n\t\u003e\u003e\u003epipe.stdout.read()\n\t5\n\t# get error message in case command executed error\n\t\u003e\u003e\u003epipe.stderr.read()\n\nthe first argument of Pipe.add function can be Cmd or SubCmd,\nplease remaind the usage of add function is changed in this case\n\n::\n\t\u003e\u003e\u003epipe = ucltip.Pipe()\n\t\u003e\u003e\u003epipe.add(Cmd('expr'), 1, '+', 3)\n\t\u003e\u003e\u003epipe.add(Cmd('sed'), 's/4/5', posix=True)\n\t\u003e\u003e\u003epipe.wait()\n\t\u003e\u003e\u003epipe.stdout.read()\n\t5\n\n::\n\n\t\u003e\u003e\u003eapt_cache = ucltip.CmdDispatcher('apt-cache')\n\t\u003e\u003e\u003epipe = ucltip.Pipe()\n\t\u003e\u003e\u003epipe.add(apt_cache.search, 'vim-common')\n\t\u003e\u003e\u003epipe.add(Cmd('grep'), 'vim')\n\t\u003e\u003e\u003epipe.wait()\n\t\u003e\u003e\u003epipe.stdout.read()\n\nHelper\n======\n\nregcmds is used to register multiple command in built-in environment one time\n\n::\n\n\t\u003e\u003e\u003eucltip.regcmds('ls', 'wget', 'sed')\n\t\u003e\u003e\u003e ls\n\tCmd object bound 'ls'\n\t\u003e\u003e\u003e wget\n\tCmd object bound 'wget'\n\t\u003e\u003e\u003e sed\n\tCmd object bound 'sed'\n\navaliabl for specify class\n\n::\n\n\t\u003e\u003e\u003eucltip.regcmds('apt-get', 'apt-cache', cls=ucltip.CmdDispatcher)\n\t\u003e\u003e\u003e apt_get\n\t\u003cucltip.CmdDispatcher object at 0xb7305dcc\u003e\n\t\u003e\u003e\u003e apt_cache\n\t\u003cucltip.CmdDispatcher object at 0xb7308bec\u003e\n\nucltip can also check the command name you want to register is in the list that\ncommand has sub command or not for reduce the class to CmdDispatcher by auto.\n\nthe list is ucltip.__CMDDISPATCHERS_LIST__, request to add new command are welcome\n\n`global_config` is used to set up global configure of All class\n\nTo change executing behavior of Cmd or CmdDispatcher\n\n::\n\t# executing command, default setting\n\t\u003e\u003e\u003eucltip.global_config(execmod='process')\n\n\t# produce command arguments only, same as dry_run\n\t\u003e\u003e\u003eucltip.global_config(execmod='list')\n\t\u003e\u003e\u003eucltip.Cmd('ls')(a=True)\n\t['ls', '-a']\n\n\t# produce command string only\n\t\u003e\u003e\u003eucltip.global_config(execmod='string')\n\t\u003e\u003e\u003eucltip.Cmd('ls')(a=True)\n\t'ls -a'\n\nTo force all command executed by os.system\n\n::\n\t\u003e\u003e\u003eucltip.global_config(via_shell=True)\n\nDebugging\n=========\n\nucltip provid debug output in /var/log/syslog after you enable debug mode\n\n::\n\t\u003e\u003e\u003e ucltip.global_config(debug=True)\n\nGet invlolved\n=============\n\nif you are interesting to help, please contact author,\nHychen, his email is  \u003cossug.hychen at gmail.com\u003e.\n\nThe VCS of code is avaliabl on  http://github.com/hychen/ucltip\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhychen%2Fpkg-ucltip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhychen%2Fpkg-ucltip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhychen%2Fpkg-ucltip/lists"}