2
0

archivebox_config.py 1.9 KB

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