the hitchhiker s the hitchhiker s guide to clis in guide
play

The Hitchhiker's The Hitchhiker's Guide to CLIs in Guide to CLIs - PowerPoint PPT Presentation

The Hitchhiker's The Hitchhiker's Guide to CLIs in Guide to CLIs in Python Python Vinayak Mehta @vortex_ape $ whoami $ whoami https://github.com/vinayak-mehta https://github.com/camelot-dev https://www.recurse.com In the beginning ...


  1. The Hitchhiker's The Hitchhiker's Guide to CLIs in Guide to CLIs in Python Python Vinayak Mehta @vortex_ape

  2. $ whoami $ whoami https://github.com/vinayak-mehta

  3. https://github.com/camelot-dev

  4. https://www.recurse.com

  5. In the beginning ... In the beginning ...

  6. https://www.youtube.com/watch?v=n-eFFd5BmpU

  7. teletype teletype

  8. (t)ele(ty)pe (t)ele(ty)pe

  9. tty tty

  10. shell shell

  11. keyboard \ \ input \ (terminal)- - - - - - - - - -(process) / / output / display

  12. keyboard \ \ input \ (terminal)- - -(termios)- - -(process) / / output / display

  13. $ man termios

  14. $ stty -a speed 38400 baud; rows 34; columns 166; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; -ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 c isig icanon iexten echo echoe echok -echonl -noflsh -xcase -

  15. $ man termios ... ICANON Enable canonical mode (described below). ...

  16. $ stty -icanon

  17. $ man termios ... ONLCR (XSI) Map NL to CR-NL on output. ...

  18. $ stty -onlcr

  19. $ man termios ... ECHO Echo input characters. ...

  20. $ stty -echo

  21. $ reset

  22. import termios

  23. Signals Signals

  24. In-band signaling In-band signaling

  25. Control characters Control characters

  26. Control characters Control characters ^H backspace ^J newline ^C interrupt the running process ^D end text input or exit the shell

  27. Escape sequences Escape sequences

  28. Escape sequences Escape sequences \u001b[2J : clear screen \u001b[1m : make text bold \u001b[31m : make text red \u001b[{n}A : moves cursor up by n

  29. Streams Streams

  30. stdin stdin

  31. stdout and stderr stdout and stderr

  32. Redirection Redirection

  33. $ echo "hello" > file $ echo "world" >> file

  34. $ echo "hello" | cat hello

  35. Command-line interfaces Command-line interfaces

  36. Command-line interfaces Command-line interfaces Prompt

  37. Command-line interfaces Command-line interfaces Prompt command

  38. Command-line interfaces Command-line interfaces Prompt command option1 option2

  39. Command-line interfaces Command-line interfaces Prompt command option1 option2 argument1 argument2 <Enter>

  40. Command-line interfaces Command-line interfaces Prompt command option1 option2 argument1 argument2 <Enter> Output

  41. Arguments Arguments

  42. Arguments Arguments $ cp src dst

  43. Options Options

  44. Options Options $ cp -r src dst

  45. Help Help

  46. Help Help $ cp --help

  47. Man pages Man pages $ man termios

  48. Standards Standards

  49. POSIX POSIX

  50. XDG base directory specification XDG base directory specification

  51. XDG base directory specification XDG base directory specification $XDG_CONFIG_HOME=$HOME/.config $XDG_DATA_HOME=$HOME/.local/share $XDG_CACHE_HOME=$HOME/.cache

  52. CLIs in Python CLIs in Python

  53. smol-pip smol-pip

  54. $ smol-pip install --upgrade package_name

  55. Standard library Standard library

  56. sys sys

  57. sys.argv sys.argv

  58. getopt getopt

  59. import sys help = "Pip Installs Packages." if __name__ == "__main__": arguments = sys.argv

  60. import sys help = "Pip Installs Packages." if __name__ == "__main__": arguments = sys.argv if arguments[1] in ["-h", "--help"]: print(help)

  61. import sys help = "Pip Installs Packages." if __name__ == "__main__": arguments = sys.argv if arguments[1] in ["-h", "--help"]: print(help) elif arguments[1] in ["-v", "--version"]: print("0.1.0")

  62. import sys help = "Pip Installs Packages." if __name__ == "__main__": arguments = sys.argv ... else : print(arguments) # ['smol-pip', 'install', '--upgrade', 'Click'] if arguments[1] == "install": # dispatch to install / upgrade code else : raise ValueError("Unknown subcommand!")

  63. optparse optparse

  64. PEP 389 PEP 389

  65. argparse argparse

  66. argparse argparse -pf -file +f +rgb /f /file

  67. argparse argparse pip install pip freeze pip search

  68. import argparse parser = argparse.ArgumentParser( description="Pip Installs Packages." )

  69. import argparse parser = argparse.ArgumentParser( description="Pip Installs Packages." ) parser.add_argument( "-v", "--version", action="version", version="0.1.0" )

  70. subparsers = parser.add_subparsers(dest="subparser_name") install = subparsers.add_parser("install")

  71. subparsers = parser.add_subparsers(dest="subparser_name") install = subparsers.add_parser("install") install.add_argument( "-u", "--upgrade", action="store_true", help="Upgrade package to the newest available version.", ) install.add_argument("package_name")

  72. if __name__ == "__main__": arguments = parser.parse_args() print(arguments) # Namespace(package_name='Click', upgrade=True)

  73. if __name__ == "__main__": arguments = parser.parse_args() print(arguments) # Namespace(package_name='Click', upgrade=True) if arguments.subparser_name == "install": # dispatch to install / upgrade code else : raise ValueError("Unknown subcommand!")

  74. $ smol-pip --help usage: smol-pip [-h] [-v] {install} ... Pip Installs Packages. positional arguments: {install} optional arguments: -h, --help show this help message and exit -v, --version show program's version number and exit

  75. Python Package Index Python Package Index

  76. docopt docopt

  77. help = """Pip Installs Packages. Usage: smol-pip install PACKAGE_NAME smol-pip install --upgrade PACKAGE_NAME Options: -h --help Show this screen. --version Show version. """

  78. from docopt import docopt if __name__ == "__main__": arguments = docopt(help, version="0.1.0") print(arguments) # {'--upgrade': True, # 'PACKAGE_NAME': 'Click', # 'install': True}

  79. from docopt import docopt if __name__ == "__main__": arguments = docopt(help, version="0.1.0") print(arguments) # {'--upgrade': True, # 'PACKAGE_NAME': 'Click', # 'install': True} if arguments["install"]: # dispatch to install / upgrade code else : raise ValueError("Unknown subcommand!")

  80. click click

  81. import click def cli (*args, **kwargs): """Pip Installs Packages.""" pass

  82. import click @click.group("pip") def cli (*args, **kwargs): """Pip Installs Packages.""" pass

  83. import click @click.group("pip") @click.version_option("0.1.0") def cli (*args, **kwargs): """Pip Installs Packages.""" pass

  84. def install (*args, **kwargs): """Install packages.""" # install / upgrade package_name

Recommend


More recommend