Skip to content

Instantly share code, notes, and snippets.

@naclsn
Last active October 9, 2021 13:03
Show Gist options
  • Save naclsn/deccea0126ab5c3ba87b3651c09e858d to your computer and use it in GitHub Desktop.
Save naclsn/deccea0126ab5c3ba87b3651c09e858d to your computer and use it in GitHub Desktop.
Python script to write helper function callable from command line, somewhat plateforme independent (even Python2).
#!/usr/bin/env python
# coding: utf-8
def default():
_print("for a list of command use `python please _help`")
_print("for help about a command use `python please _help,command`")
def do_example1():
""" This is an example.
Use `please do_example` (or `python please do_example` on Windows)
to execute this function.
"""
out = _path('tmp', 'out')
if not _exists(out):
_mkdir(out)
_cd(out)
_print(_cwd())
def do_example2(argA, argB, *args):
""" This is an example with arguments.
Every positional argument must be provided through command line:
`please do_example2,valueA,valueB` -> argA=valueA, argB=valueB, args=()
`please do_example2,valueA` -> "missing 1 required positional argument"
`please do_example2,valueA,valueB,more,values` -> argA=valueA, argB=valueB, args=(more,values)
"""
_print("argA:", argA)
_print("argB:", argB)
_print("args:", args)
#--- python please do (v1.2)
try:
_print=eval("print")
except Exception:
def _print(*va,**kwa):print(kwa).get('sep'," ").join([str(v)for v in va])+kwa.get('end',"\n"),
_defined=list(filter(lambda s:not s.startswith('_'),globals().keys()));import os,sys,shutil;_exit=sys.exit;_path=os.path.join;_abspath=os.path.abspath;_relpath=os.path.relpath;_cd=os.chdir;_cwd=os.getcwd;_ls=os.listdir;_walk=os.walk;_mv=os.rename;_rm=os.remove;_cp=shutil.copy2;_isfile=os.path.isfile;_isdir=os.path.isdir;_mkdir=os.makedirs;_exists=os.path.exists;_cptree=lambda src,dst,ignore=lambda path,name:False:(_exists(dst)or not _mkdir(dst))and[_cptree(s,d,ignore)if _isdir(s)and(_exists(d)or not _mkdir(d))else _cp(s,d)for s,d in((_path(src, n),_path(dst, n))for n in _ls(src)if not ignore(src,n))];_rmtree=shutil.rmtree;_system=os.system;_system_verb=lambda command:[_print(command),os.system(command)][1];_nop=lambda:None
_help=lambda name="":help(globals()[name])if name else _print(*_defined)
_please=lambda dos:dict([(d[0],globals()[d.pop(0)](*d))for d in[do.split(",")for do in dos]])
if'__main__'==__name__:_=_please(sys.argv[1:]or['default']);_exit(next(iter(filter(None,_.values())),0)and _)
# This can be added to the "profile.ps1".
# Custom completer for Python scripts like "python please [tab or ctrl+space]"
$_pyCompleterScriptBlock = {
param ($commandName, $wordToComplete, $cursorPosition)
$elements = $wordToComplete.CommandElements
$script = $wordToComplete.CommandElements[1].Value
switch ($script) {
{ $_ -ieq '.\please' -or $_ -ieq 'please' } {
$word = $elements[$elements.Count-1].Value
$please_help = python please _help
$please_help = $please_help -split ' '
if ($word -ieq $script) {
$please_help
} else {
$please_help | Where-Object { $_ -like "$word*" }
}
}
Default {}
}
}
Register-ArgumentCompleter -Native -CommandName py -ScriptBlock $_pyCompleterScriptBlock
Register-ArgumentCompleter -Native -CommandName python -ScriptBlock $_pyCompleterScriptBlock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment