{ "info": { "author": "Yudhajit N", "author_email": "28938427+Sh3llcod3@users.noreply.github.com", "bugtrack_url": null, "classifiers": [ "Environment :: Console", "License :: OSI Approved :: MIT License", "Operating System :: POSIX :: Linux", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "# Easyparse\n\nEasyparse is a command line argument parser. It is extremely lightweight.\nIt has no external dependencies. Easyparse can not only handle the argument\nparsing in your programs, but it can also handle displaying basic help screens\nand error-handling. It's significantly easier to use than other argument parsers,\nas it favours simplicity over complexity or advanced features.\n\n## Setup & Prerequsites\n\n- `python 3.6` or above\n- `pip`\n\nStart by installing the module using pip.\n```shell\n$ python3 -m pip install easyparse\n```\n\n## Initialising\n\nIn your program, import the module, then create an instance of the opt_parser class.\n```Python\nimport easyparse\n\nparser = easyparse.opt_parser()\n```\n\nFrom this point forward, we will use our `parser` instance to demonstrate\nany features or examples.\n\nThe opt_parser accepts two optional flags.\n`parser = easyparse.opt_parser(argument_list=sys.argv, show_colors=True)`\n\nThe `argument_list` flag takes a list. This is where the arguments will be\nparsed from. By default this is sys.argv. For most programs this is where\nyou will want to take the arguments in from, but you are free to use any\nlist you wish.\n\nThe `show_colors=True` flag can be set to True or False, depending on\nwhether you want to enable the default colours when displaying the help\nscreen, displaying errors and such. Unless your terminal doesn't support\ncolour, there is usually no reason to turn this setting off.\n\n## Adding arguments\n\nEasyparse works by you first adding arguments, then calling the `parse_args` function\nwhich then looks at `argument_list` and parses the arguments from that list. Therefore,\nto start parsing arguments, you must first add the arguments that you are looking to parse.\n\n```Python\nparser.add_arg(\"\", \"\", \"\", \"\", )\n```\n\nHere's an example of how you would add the default help argument.\n\n```Python\nparser.add_arg(\"-h\", \"--help\", None,\"Show this help screen and exit.\", optional=True)\n```\nTo add an argument, you have to add the short form, for it to count as a valid argument.\nNow you may wonder, ***'what if I wish to add an argument that only has a short form and but\nlacks one or more of the other variables?'***\n\nThis can be easily achieved by inserting a `None` in the position of the variable,\nor explicitly specifying what the variable is representing like this `variable=value`\nFor example, if I wanted to add such an argument that doesn't require one or more values,\nI could do it like these few examples.\n\n```Python\nparser.add_arg(\"-s\", None, None, \"Turn on some option.\", optional=True)\nparser.add_arg(\"-n\", \"--no-foobar\", None, \"Turn off some other option.\")\nparser.add_arg(\"-f\", None, \"file\", description=\"Read values from file.\")\nparser.add_arg(\"-c\", meta_var=\"ciphertext\", description=\"Lorem ipsum dolor sit amet.\")\nparser.add_arg(\"-v\", \"--version\", description=\"Lorem ipsum dolor sit amet.\")\n```\n\nEasyparse is quite flexible, as to what characters can be used as long as\nthe argument follows a certain format. **Short hand arguments need to\nstart with `-` and have only character after that.** The character can\nbe an alphabet or punctuation mark like `-?`, or it can be any single digit number,\n`-1`. **Full form arguments need to start with `--` and can be of any length\ncontaining any printable characters.**\n\nThe only exception to this is the final variable `optional=False`.\nYou don't have to specify this option, unless you wish to mark the\nargument as optional. When you display the help screen, it will show\nup under the `Optional arguments` section.\n\n## Adding comments\n\nAfter adding all the arguments, you may wish to add some comments,\nto describe how your program works, or to convey other information\nto the user. These comments will be displayed alongside the usage\noption in the help screen, when the help screen is called.\nAdding a comment is quite simple, using our `parser` instance,\nit can be done like this.\n\n```Python\nparser.add_comment(\"Hey, look at me, I'm a comment!\")\nparser.add_comment(\"I'm another comment. Cool, huh?\")\n```\n\n## Adding examples\n\nLike comments, you can add usage examples, that will show\nup at the top of the help screen. **Note: You do not\nneed to specify the file-name when adding examples,\nit will be prepended automatically**. You can override\nthis behaviour by specifying a string, ideally a filename,\nin the second variable, if you wish. You can add examples like this.\n\n```Python\nparser.add_example(\"-s -e -i foo -k bar\")\nparser.add_example(\"-d -n 32 -r 128 -i 'Lorem ipsum dolor' \")\nparser.add_example(\"\", prepend_name=\"\")\nparser.add_example(\"./file-name \", str()) # Nothing prepended\n```\n\n## Parsing arguments\n\nOnce we have setup our arguments, we need to parse them.\nLet's use the same `parser` instance as above.\n\n```Python\nparser.parse_args()\n```\n\nOnce the `parse_args` function is called the arguments will be parsed\nand a dictionary will be created. This will allow you to see which\nspecific arguments have been set and obtain values from arguments\nwhich need a value passed to it. It is at this point, that if any\nerrors have been detected. It will display an error and exit showing\nwhich error it encountered and with which argument.\n\n## Checking presence\n\nTo check if an argument has been supplied, we can use the `is_present`\nfunction. You can use either the short form or long form of the\nargument to check presence. The function will return `True` if\nthe argument is set. If the argument is not set, it will return\n`None`. *Note: In some cases it could return False instead of None.\nThis shouldn't happen, but if it does, please report this as an issue,\nstating which arguments you supplied for it to cause this.*\n\n```Python\n# Check if the help flag is set\n# Any form can be used. Both will result\n# In True if present.\n\nfoo = parser.is_present(\"-h\")\nbar = parser.is_present(\"--help\")\nother_option = parser.is_present(\"-v\")\nnonexistent_option = parser.is_present(\"-z\")\n\n\nprint(foo)\nprint(bar)\nprint(other_option)\nprint(nonexistent_option)\n```\n\nLet's invoke our program with -h\n\n```shell\n$./program -h\nTrue\nTrue\nNone\nNone\n\n$./program -v\nNone\nNone\nTrue\nNone\n```\n\nOnce we have checked presence, we need to get the values\nfrom the present arguments, which we can use to create\na functional program.\n\n## Checking multiple\n\nImagine having to do `parser.is_present()` for every\nargument you want to check. That would get redundant\nvery quickly. Fortunately, with the `check_multiple`\nfunction. `check_multiple` takes any number of arguments\nwith a final value called `sep`\n\nTo check if multiple arguments are present, we can\ndo this.\n\n```Python\nparser.check_multiple(\"-s\", \"-e\", \"-i\", \"-k\")\n```\nIt doesn't matter if the argument is the short hand or\nthe long form. It will either return `True` if all\nthe arguments have been supplied or it will return\n`False` if one or more are not supplied.\n\nYou may also be wondering, ***What if I wanted to\nsee presence for each argument manually?***\nThat's where the `sep=` comes in.\nif `sep` is set to `True` a list will be returned\ncontaining a `True/False` depending on whether the argument\nis present, instead of a boolean value. We can achieve this\nin the examples following.\n\n```Python\nparser.check_multiple(\"-s\", \"-x\", \"-f\", \"-k\", sep=True)\n```\n\nLet's create a small demonstration, using our `parser`\ninstance from earlier.\n\n```Python\nx = parser.check_multiple(\"-s\", \"-e\", \"-i\", \"-k\")\ny = parser.check_multiple(\"-s\", \"-x\", \"-f\", \"-k\", sep=True)\nprint(x)\nprint(y)\n```\n\nLet's run our program with some arguments and verify\nthat it works.\n\n```shell\n./program -se -i \"lorem ipsum\" -k \"dolor sit amet\"\nTrue\n[True, False, False, True]\n```\n\n## Getting values\n\nThe whole purpose of an argument parser is so we can pick\nup supplied values. We can do that easily here, like this.\n\n```Python\ninput_value = parser.value_of(\"-i\")\nnonexistent_value = parser.value_of(\"-f\")\n\nprint(\"You said: \", input_value)\nprint(\"This should return be None: \", non_existent_value)\n```\n\nLet's test it out.\n\n```shell\n$./program -i 'Hello!'\nYou said Hello!\nThis should be None: None\n```\n\n## Help screen\n\nIn programs which you require the user to supply command-line\narguments, chances are, you would need a help screen, to\ndisplay the possible arguments your program can take.\nEasyparse makes this process quite simple. Every time you\nadd an argument, it automatically gets added to the help\nscreen. All you need to do is simply display that screen when\nyou want, perhaps when the user supplies `-h` or `--help`.\n\n```Python\n#Let's create a small program to demo this.\n#program.py\n\nimport sys\nimport easyparse\nparser = easyparse.opt_parser()\n\nparser.add_arg(\"-h\", \"--help\", None,\"Show this help screen and exit.\", optional=True)\nparser.add_arg(\"-v\", \"--version\", None,\"Print version information and exit.\", optional=True)\nparser.add_arg(\"-s\", \"--substitution\", None,\"lorem ipsum dolor\")\nparser.add_arg(\"-e\", \"--enc-type\", None,\"lorem ipsum dolor\")\nparser.add_arg(\"-x\", \"--dec\", None,\"lorem ipsum dolor\")\nparser.add_arg(\"-c\", \"--ciphertext\", \"ciphertext\", description=\"lorem ipsum dolor\")\nparser.add_arg(\"-k\", \"--key\", \"key\", \"lorem ipsum dolor\")\n\nif len(sys.argv) == 1 or parser.is_present(\"-h\"):\n # Change to True to add a space between each argument\n parser.show_help(add_space=False)\n\n # An alternate help screen can also be viewed.\n parser.view_args()\n\n```\n\nLet's run our program.\n\n```shell\n\n$./program.py --help\n[+] Usage: ./program.py [options]\n\n[i] Positional arguments:\n\n -s --substitution\n lorem ipsum dolor\n -e --enc-type\n lorem ipsum dolor\n -x --dec\n lorem ipsum dolor\n -c --ciphertext ciphertext\n lorem ipsum dolor\n -k --key key\n lorem ipsum dolor\n\n[i] Optional arguments:\n\n -h --help\n Show this help screen and exit.\n -v --version\n Print version information and exit.\n\n```\n\nSimilarly, if any comments or examples were\nadded, they would show up too. Let's keep it\nsimple for the example, though, so we'll do that\nlater.\n\n## Error handling\n\nEasyparse should be able to catch these types\nof common errors, regardless of whether the long\nor short form has been supplied, or different forms\nhave been mixed:\n\n- Duplicate arguments\n- Invalid arguments\n- No values passed where required\n- Invalid argument type\n\nLet's see a few cases of these.\n\n```shell\n$./program -s -s\n[!] ./unit_tests.py: Duplicate arguments supplied.\n\n$./program -i\n[!] ./unit_tests.py: Option: '-i' missing value\n\n./program -l\n[!] ./unit_tests.py: Invalid option: -l\n```\n\n## Summary\n\nLet's summarise all the functions we have covered so far:\n\n```Python\n# Import the parser\nimport sys\nimport easyparse\n\n# initialise an instance of the parser.\n# Both the arguments are optional.\nparser = easyparse.opt_parser(argument_list=sys.argv, show_colors=True)\n\n# Add an argument\nparser.add_arg(\"-h\", \"--help\", None,\"Show this help screen and exit.\", optional=True)\n\n# Add a comment\nparser.add_comment(\"Lorem ipsum dolor sit amet.\")\n\n# Add an example\nparser.add_example(\"\", prepend_name=\"file-name\")\n\n# Once everything is set parse the arguments\nparser.parse_args()\n\n# show the help screen\n# Both -h and --help can be used\n# The help screen will be displayed regardless\n# of which form is used.\nif len(sys.argv) == 1 or parser.is_present(\"--help\"):\n parser.show_help()\n\n```\n\n## Conclusion\n\nI would like to thank you for taking the time to read\nthis documentation, I hope someone finds this basic project useful.\nIf you have any questions at all, please create an issue.\n\nYou can view my other projects at my [GitHub page](https://github.com/Sh3llcod3/).\n\n[GitHub Link](https://github.com/sh3llcod3/Easyparse)\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Sh3llcod3/Easyparse", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "easyparse", "package_url": "https://pypi.org/project/easyparse/", "platform": "", "project_url": "https://pypi.org/project/easyparse/", "project_urls": { "Homepage": "https://github.com/Sh3llcod3/Easyparse", "Repository": "https://github.com/Sh3llcod3/Easyparse" }, "release_url": "https://pypi.org/project/easyparse/1.0.5/", "requires_dist": null, "requires_python": ">=3.6,<4.0", "summary": "A user-friendly, lightweight command-line argument parser.", "version": "1.0.5" }, "last_serial": 5936373, "releases": { "1.0.3": [ { "comment_text": "", "digests": { "md5": "ebeeff708b8e86a142f4332551cf8ab5", "sha256": "6d3c7ecc29abbfc07a2e7a60888d468ed2923dc01d889c3ac401abdd0bb854cf" }, "downloads": -1, "filename": "easyparse-1.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "ebeeff708b8e86a142f4332551cf8ab5", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 11821, "upload_time": "2018-07-22T18:51:33", "url": "https://files.pythonhosted.org/packages/52/83/54894bc56bada3840adc6aaf1b37223f9198854c8413748cfdc4dab54a05/easyparse-1.0.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "f5cd3981740cd528a79f79fafe378312", "sha256": "77988f31ece1bf24818f72655c1596d0299e3099f02f42a40f3e4f4d0801fb64" }, "downloads": -1, "filename": "easyparse-1.0.3.tar.gz", "has_sig": false, "md5_digest": "f5cd3981740cd528a79f79fafe378312", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15217, "upload_time": "2018-07-22T18:51:34", "url": "https://files.pythonhosted.org/packages/60/27/8e07439876f0577f8f9ea8e05ba91c1f96df1fe0b5f77a3e4d1b817ec98b/easyparse-1.0.3.tar.gz" } ], "1.0.4": [ { "comment_text": "", "digests": { "md5": "c2f51fae58654269bab6bcb202854c46", "sha256": "21dbf8954d6b1025bc02ea3013c99ab05e650754128cb8e046359cfbb2f42d21" }, "downloads": -1, "filename": "easyparse-1.0.4-py3-none-any.whl", "has_sig": false, "md5_digest": "c2f51fae58654269bab6bcb202854c46", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 11854, "upload_time": "2018-08-01T23:24:41", "url": "https://files.pythonhosted.org/packages/3d/d3/89f99d6654a713a430a885fe79a39bbba6305652a820da54a829bc310ad9/easyparse-1.0.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "91d944d6860c3be3507bd6e8ef404d4f", "sha256": "2e2c25af756b98757ec8a44b946007b5c3a47e52b0b6760147d75f227e3242c2" }, "downloads": -1, "filename": "easyparse-1.0.4.tar.gz", "has_sig": false, "md5_digest": "91d944d6860c3be3507bd6e8ef404d4f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15297, "upload_time": "2018-08-01T23:24:42", "url": "https://files.pythonhosted.org/packages/00/1c/713194585e117edfc88e0bc5a4b1c13328fdbc82f50a129a5af76d4c7e96/easyparse-1.0.4.tar.gz" } ], "1.0.5": [ { "comment_text": "", "digests": { "md5": "2dab11c86b9692a8563360a9cb7e4029", "sha256": "27fd3818b0a990c60be07ee516747accdbf2263e1deeece1c1021d60674f2be6" }, "downloads": -1, "filename": "easyparse-1.0.5-py3-none-any.whl", "has_sig": false, "md5_digest": "2dab11c86b9692a8563360a9cb7e4029", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6,<4.0", "size": 10681, "upload_time": "2019-10-06T23:42:39", "url": "https://files.pythonhosted.org/packages/fd/c2/74d3095507bd8926515cf0c036de721d97d043f490643043337650606877/easyparse-1.0.5-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "0913fb76b82d831e4742d3078e000645", "sha256": "d0093d644234e1f9b54df578229a42f700355a5a36798afed089b281d2ab67ff" }, "downloads": -1, "filename": "easyparse-1.0.5.tar.gz", "has_sig": false, "md5_digest": "0913fb76b82d831e4742d3078e000645", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6,<4.0", "size": 14919, "upload_time": "2019-10-06T23:42:41", "url": "https://files.pythonhosted.org/packages/94/9c/da38050b9fc87844eb6c30e0cce9e9dcfab74451194ebbe5bde60f2f9ea1/easyparse-1.0.5.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "2dab11c86b9692a8563360a9cb7e4029", "sha256": "27fd3818b0a990c60be07ee516747accdbf2263e1deeece1c1021d60674f2be6" }, "downloads": -1, "filename": "easyparse-1.0.5-py3-none-any.whl", "has_sig": false, "md5_digest": "2dab11c86b9692a8563360a9cb7e4029", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6,<4.0", "size": 10681, "upload_time": "2019-10-06T23:42:39", "url": "https://files.pythonhosted.org/packages/fd/c2/74d3095507bd8926515cf0c036de721d97d043f490643043337650606877/easyparse-1.0.5-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "0913fb76b82d831e4742d3078e000645", "sha256": "d0093d644234e1f9b54df578229a42f700355a5a36798afed089b281d2ab67ff" }, "downloads": -1, "filename": "easyparse-1.0.5.tar.gz", "has_sig": false, "md5_digest": "0913fb76b82d831e4742d3078e000645", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6,<4.0", "size": 14919, "upload_time": "2019-10-06T23:42:41", "url": "https://files.pythonhosted.org/packages/94/9c/da38050b9fc87844eb6c30e0cce9e9dcfab74451194ebbe5bde60f2f9ea1/easyparse-1.0.5.tar.gz" } ] }