{"id":15922052,"url":"https://github.com/devforfu/ancli","last_synced_at":"2025-04-03T12:22:45.942Z","repository":{"id":57410275,"uuid":"194147826","full_name":"devforfu/ancli","owner":"devforfu","description":"Building argument parser from a function annotation","archived":false,"fork":false,"pushed_at":"2019-10-17T19:44:56.000Z","size":84,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-10T05:55:21.201Z","etag":null,"topics":["argparse","cli","package","pip","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/ancli/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devforfu.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-27T18:54:21.000Z","updated_at":"2020-10-24T16:08:14.000Z","dependencies_parsed_at":"2022-09-04T23:00:28.120Z","dependency_job_id":null,"html_url":"https://github.com/devforfu/ancli","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devforfu%2Fancli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devforfu%2Fancli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devforfu%2Fancli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devforfu%2Fancli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devforfu","download_url":"https://codeload.github.com/devforfu/ancli/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246998821,"owners_count":20866812,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["argparse","cli","package","pip","python"],"created_at":"2024-10-06T20:04:25.251Z","updated_at":"2025-04-03T12:22:45.924Z","avatar_url":"https://github.com/devforfu.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"ancli\n=====\n\n.. image:: https://i.imgur.com/5hCKc9u.png\n    :target: https://i.imgur.com/5hCKc9u.png\n    :align: center\n\nBuilding argument parser from a function annotation. A simple utility inspired by\n``Fire`` and ``docopt``. Ad-hoc solution for someone who often writes scripts with a\nsingle entry point.\n\nHow?\n----\n\nThe process of building CLI with ``ancli`` is very simple.\n\n1. Write a plain Python function with annotated parameters.\n2. Wrap it with ``make_cli``.\n3. Run your script.\n\n\nExamples\n--------\n\n1. Function with annotated parameters\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nThe function ``run`` has explicitly annotated parameters and its signature is used\nto instantiate ``argparse.ArgumentParser`` instance that accepts parameters with\nspecific types and default (if any) parameters. If default value is not provided,\nthen the parameter is considered to be required.\n\n.. code:: python\n\n    from ancli import make_cli\n\n    def run(path: str, flag: bool = True, iterations: int = 1):\n        print(f'run: path={path}, flag={flag}, iterations={iterations}')\n\n    if __name__ == '__main__':\n        make_cli(run)\n\nNow this snippet can be used as follows.\n\n.. code:: bash\n\n    $ python script.py --path file.txt --flag 0\n    run: path=file.txt, flag=False, iterations=1\n\n\n2. Function without annotations\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nThe functions without type annotations try to infer the parameters types based\non their default values.\n\n.. code:: python\n\n    from ancli import make_cli\n\n    def run(a, b=2, c=3.0):\n        for param in (a, b, c):\n            print(type(param))\n\n    if __name__ == '__main__':\n        make_cli(run)\n\nThe parameters without default values are considered as strings.\n\n.. code:: bash\n\n    $ python script.py --a 1 --b 2 --c 3.0\n    \u003ctype 'str'\u003e\n    \u003ctype 'int'\u003e\n    \u003ctype 'float'\u003e\n\n3. Running ``ancli`` as a module\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nRunning package as a module allows to dynamically build a CLI from some function. \nYou just need to specify a path to the module, and function which should be \ntreated as an entry point.\n\n.. code:: bash\n\n    $ python -m ancli examples.functions:compute --a 2 --b 6\n    42\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevforfu%2Fancli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevforfu%2Fancli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevforfu%2Fancli/lists"}