archivebox_config.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. __package__ = 'archivebox.cli'
  3. __command__ = 'archivebox config'
  4. import sys
  5. import argparse
  6. from typing import Optional, List, IO
  7. from ..main import config
  8. from ..util import docstring
  9. from ..config import OUTPUT_DIR
  10. from ..logging_util import SmartFormatter, accept_stdin
  11. @docstring(config.__doc__)
  12. def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional[str]=None) -> None:
  13. parser = argparse.ArgumentParser(
  14. prog=__command__,
  15. description=config.__doc__,
  16. add_help=True,
  17. formatter_class=SmartFormatter,
  18. )
  19. group = parser.add_mutually_exclusive_group()
  20. group.add_argument(
  21. '--get', #'-g',
  22. action='store_true',
  23. help="Get the value for the given config KEYs",
  24. )
  25. group.add_argument(
  26. '--set', #'-s',
  27. action='store_true',
  28. help="Set the given KEY=VALUE config values",
  29. )
  30. group.add_argument(
  31. '--reset', #'-s',
  32. action='store_true',
  33. help="Reset the given KEY config values to their defaults",
  34. )
  35. parser.add_argument(
  36. 'config_options',
  37. nargs='*',
  38. type=str,
  39. help='KEY or KEY=VALUE formatted config values to get or set',
  40. )
  41. command = parser.parse_args(args or ())
  42. config_options_str = ''
  43. if not command.config_options:
  44. config_options_str = accept_stdin(stdin)
  45. config(
  46. config_options_str=config_options_str,
  47. config_options=command.config_options,
  48. get=command.get,
  49. set=command.set,
  50. reset=command.reset,
  51. out_dir=pwd or OUTPUT_DIR,
  52. )
  53. if __name__ == '__main__':
  54. main(args=sys.argv[1:], stdin=sys.stdin)