developing-builtin-kittens.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. Developing builtin kittens
  2. =============================
  3. Builtin kittens in kitty are written in the Go language, with small Python
  4. wrapper scripts to define command line options and handle UI integration.
  5. Getting started
  6. -----------------------
  7. To get started with creating a builtin kitten, one that will become part of kitty
  8. and be available as ``kitten my-kitten``, create a directory named
  9. :file:`my_kitten` in the :file:`kittens` directory. Then, in this directory
  10. add three, files: :file:`__init__.py` (an empty file), :file:`__main__.py` and
  11. :file:`main.go`.
  12. Template for `main.py`
  13. ^^^^^^^^^^^^^^^^^^^^^^
  14. The file :file:`main.py` contains the command line option definitions for your kitten. Change the actual options and help text below as needed.
  15. .. code-block:: python
  16. #!/usr/bin/env python
  17. # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
  18. import sys
  19. # See the file kitty/cli.py in the kitty sourcecode for more examples of
  20. # the syntax for defining options
  21. OPTIONS = r'''
  22. --some-string-option -s
  23. default=my_default_value
  24. Help text for a simple option taking a string value.
  25. --some-boolean-option -b
  26. type=bool-set
  27. Help text for a boolean option defaulting to false.
  28. --some-inverted-boolean-option
  29. type=bool-unset
  30. Help text for a boolean option defaulting to true.
  31. --an-integer-option
  32. type=int
  33. default=13
  34. bla bla
  35. --an-enum-option
  36. choices=a,b,c,d
  37. default=a
  38. This option can only take the values a, b, c, or d
  39. '''.format
  40. help_text = '''\
  41. The introductory help text for your kitten.
  42. Can contain multiple paragraphs with :bold:`bold`
  43. :green:`colored`, :code:`code`, :link:`links <http://url>` etc.
  44. formatting.
  45. Option help strings can also use this formatting.
  46. '''
  47. # The usage string for your kitten
  48. usage = 'TITLE [BODY ...]'
  49. short_description = 'some short description of your kitten it will show up when running kitten without arguments to list all kittens`
  50. if __name__ == '__main__':
  51. raise SystemExit('This should be run as kitten my-kitten')
  52. elif __name__ == '__doc__':
  53. cd = sys.cli_docs # type: ignore
  54. cd['usage'] = usage
  55. cd['options'] = OPTIONS
  56. cd['help_text'] = help_text
  57. cd['short_desc'] = short_description
  58. Template for `main.go`
  59. ^^^^^^^^^^^^^^^^^^^^^^
  60. .. code-block:: go
  61. package my_kitten
  62. import (
  63. "fmt"
  64. "kitty/tools/cli"
  65. )
  66. var _ = fmt.Print
  67. func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
  68. // Here rc is the exit code for the kitten which should be 1 or higher if err is not nil
  69. fmt.Println("Hello world!")
  70. fmt.Println(args)
  71. fmt.Println(fmt.Sprintf("%#v", opts))
  72. return
  73. }
  74. func EntryPoint(parent *cli.Command) {
  75. create_cmd(parent, main)
  76. }
  77. Edit :file:`tools/cmd/tool/main.go`
  78. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  79. Add the entry point of the kitten into :file:`tools/cmd/tool/main.go`.
  80. First, import the kitten into this file. To do this, add :code:`"kitty/kittens/my_kitten"` into the :code:`import ( ... )` section at the top.
  81. Then, add ``my_kitten.EntryPoint(root)`` into ``func KittyToolEntryPoints(root *cli.Command)`` and you are done. After running make you should
  82. be able to test your kitten by running::
  83. kitten my-kitten