__main__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  2. # :Copyright: © 2020, 2022 Günter Milde.
  3. # :License: Released under the terms of the `2-Clause BSD license`_, in short:
  4. #
  5. # Copying and distribution of this file, with or without modification,
  6. # are permitted in any medium without royalty provided the copyright
  7. # notice and this notice are preserved.
  8. # This file is offered as-is, without any warranty.
  9. #
  10. # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
  11. #
  12. # Revision: $Revision: 9064 $
  13. # Date: $Date: 2022-06-10 13:08:13 +0200 (Fr, 10. Jun 2022) $
  14. """Generic command line interface for the `docutils` package.
  15. See also
  16. https://docs.python.org/3/library/__main__.html#main-py-in-python-packages
  17. """
  18. import argparse
  19. import locale
  20. import sys
  21. import docutils
  22. from docutils.core import Publisher, publish_cmdline, default_description
  23. class CliSettingsSpec(docutils.SettingsSpec):
  24. """Runtime settings & command-line options for the generic CLI.
  25. Configurable reader, parser, and writer components.
  26. The "--writer" default will change to 'html' when this becomes
  27. an alias for 'html5'.
  28. """
  29. settings_spec = (
  30. 'Docutils Application Options',
  31. 'Reader, writer, and parser settings influence the available options. '
  32. ' Example: use `--help --writer=latex` to see LaTeX writer options. ',
  33. # options: ('help text', [<option strings>], {<keyword arguments>})
  34. (('Reader name (currently: "%default").',
  35. ['--reader'], {'default': 'standalone', 'metavar': '<reader>'}),
  36. ('Parser name (currently: "%default").',
  37. ['--parser'], {'default': 'rst', 'metavar': '<parser>'}),
  38. ('Writer name (currently: "%default").',
  39. ['--writer'], {'default': 'html5', 'metavar': '<writer>'}),
  40. )
  41. )
  42. config_section = 'docutils application'
  43. config_section_dependencies = ('docutils-cli application', # back-compat
  44. 'applications')
  45. def main():
  46. """Generic command line interface for the Docutils Publisher.
  47. """
  48. locale.setlocale(locale.LC_ALL, '')
  49. description = ('Convert documents into useful formats. '
  50. + default_description)
  51. # Update component selection from config file(s)
  52. components = Publisher().get_settings(settings_spec=CliSettingsSpec)
  53. # Update component selection from command-line
  54. argparser = argparse.ArgumentParser(add_help=False, allow_abbrev=False)
  55. argparser.add_argument('--reader', default=components.reader)
  56. argparser.add_argument('--parser', default=components.parser)
  57. argparser.add_argument('--writer', default=components.writer)
  58. # other options are parsed in a second pass via `publish_cmdline()`
  59. (args, remainder) = argparser.parse_known_args()
  60. # Ensure the current component selections are shown in help:
  61. CliSettingsSpec.settings_default_overrides = args.__dict__
  62. try:
  63. publish_cmdline(reader_name=args.reader,
  64. parser_name=args.parser,
  65. writer_name=args.writer,
  66. settings_spec=CliSettingsSpec,
  67. description=description,
  68. argv=remainder)
  69. except ImportError as error:
  70. print('%s.' % error, file=sys.stderr)
  71. if '--traceback' in remainder:
  72. raise
  73. else:
  74. print('Use "--traceback" to show details.')
  75. if __name__ == '__main__':
  76. if sys.argv[0].endswith('__main__.py'):
  77. # fix "usage" message
  78. sys.argv[0] = '%s -m docutils' % sys.executable
  79. main()