def _save_task_file(savepath, config, task): n_s = configargparse.Namespace() n_s.task = task.id parser = configargparse.Parser() parser.add_argument("--task") parser.add_argument("--database") parsed = parser.parse_args( args=[f"--task={task.id}", f"--database={config.url_path}"]) parser.write_config_file(n_s, [savepath])
def test_snake_case_fail(): """The regular YAML parser will cause ConfigArgParse to fail.""" parser = configargparse.Parser( config_file_parser_class=configargparse.YAMLConfigFileParser, ) parser.add_argument("-c", default="config.yaml", is_config_file=True) parser.add_argument("--snake-key", default=0.1) parser.add_argument("--sausage-key", default=0.2) known, unknown = parser.parse_known("") assert unknown == [] assert known == argparse.Namespace(snake_key="1.0", sausage_key="2.0", c="config.yaml")
# This is a sample Python script. import requests from bs4 import BeautifulSoup from wordcloud import WordCloud, ImageColorGenerator import matplotlib.pyplot as plt from PIL import Image import numpy as np from scipy.ndimage import gaussian_gradient_magnitude import configargparse parser = configargparse.Parser() parser.add("--url", type=str, required=True, help="Path to url") parser.add("--image-file", type=str, required=True, help="Path to image file") if __name__ == '__main__': args = parser.parse_args() url = args.url image_file = args.image_file headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Max-Age': '1800', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'
def get_options(): """ Parse and return command line options using configargparse """ # use XDG variables if they are set and available to the user # otherwise use sensible defaults conf_root = environ.get('XDG_CONFIG_HOME') or '~/.config' cache_root = environ.get('XDG_CACHE_HOME') or expanduser('~/.cache') # create default config file and cache dir names using app name conf_files = '{}/rofi-mopidy/*.yml'.format(conf_root) cache_dir = '{}/rofi-mopidy'.format(cache_root) p = configargparse.Parser(default_config_files=[conf_files]) # general options p.add('--config', '-c', is_config_file=True, help='config file path') p.add('--cache-dir', '-C', default=cache_dir, help='app cache dir') p.add('--source', '-s', action='append', required=True, choices=['local', 'spotify'], help='different sources to scan/display') p.add('--mode', '-m', choices=['albums', 'songs'], default='albums', help='whether to show albums or songs in rofi') p.add('--refresh', '-r', action='store_true', default=False, help='refresh album cache') p.add('--sorting', choices=['mtime', 'artist', 'album', 'title'], default='title', help='key to sort rofi listing on') p.add('--reverse', action='store_true', default=False, help='sort rofi entries in reverse') # local source options p.add('--local-dir', help='local music directory') # spotify source options p.add('--spotify-username', help='spotify username') p.add('--spotify-client-id', help='spotify client id') p.add('--spotify-client-secret', help='spotify secret') # rofi options p.add('--no-rofi', action='store_true', default=False, help='don\'t show rofi (only works with -r)') p.add('--use-icons', '-i', action='store_true', default=False, help='use nerdfont icons in rofi') # mopidy options p.add('--mopidy-host', default='localhost', help='host that the mopidy server is running at') p.add('--mopidy-port', default=6600, type=int, help='port that the mopidy server is running on') options = p.parse() # Error if source specific options are not set if 'local' in options.source and not options.local_dir: p.error('use of local source requires local-dir to be set') spotify_options = (options.spotify_username, options.spotify_client_id, options.spotify_client_secret) if 'spotify' in options.source and not all(spotify_options): p.error('use of spotify source requires api options to be set') return options