{"id":23635351,"url":"https://github.com/0382/argparse-f","last_synced_at":"2026-02-03T15:04:00.067Z","repository":{"id":118757768,"uuid":"595477395","full_name":"0382/argparse-f","owner":"0382","description":"Modern Fortran command line parser, implemented with OOP.","archived":false,"fork":false,"pushed_at":"2024-08-17T14:40:01.000Z","size":57,"stargazers_count":23,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-01-20T13:58:05.841Z","etag":null,"topics":["argparse","command-line","fortran","modern-fortran","oop"],"latest_commit_sha":null,"homepage":"","language":"Fortran","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/0382.png","metadata":{"files":{"readme":"README.md","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-01-31T06:43:54.000Z","updated_at":"2024-10-18T19:02:12.000Z","dependencies_parsed_at":"2023-09-24T12:00:53.436Z","dependency_job_id":"21d3c56a-6019-448f-809d-676e640a7b4f","html_url":"https://github.com/0382/argparse-f","commit_stats":{"total_commits":19,"total_committers":2,"mean_commits":9.5,"dds":"0.42105263157894735","last_synced_commit":"ab2e4c8a7b2a8ab89fb39a63a3597d5892818284"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/0382/argparse-f","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0382%2Fargparse-f","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0382%2Fargparse-f/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0382%2Fargparse-f/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0382%2Fargparse-f/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0382","download_url":"https://codeload.github.com/0382/argparse-f/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0382%2Fargparse-f/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29047800,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-03T14:55:20.264Z","status":"ssl_error","status_checked_at":"2026-02-03T14:55:19.725Z","response_time":96,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","command-line","fortran","modern-fortran","oop"],"created_at":"2024-12-28T05:34:07.122Z","updated_at":"2026-02-03T15:04:00.047Z","avatar_url":"https://github.com/0382.png","language":"Fortran","funding_links":[],"categories":[],"sub_categories":[],"readme":"# argparse-f\r\n\r\nModern Fortran command line parser, implemented with OOP.\r\n\r\n## Example\r\n```fortran\r\nprogram test\r\n    use iso_fortran_env, only: stdout =\u003e output_unit\r\n    use argparse\r\n    implicit none\r\n    character(len=10), parameter :: program_name = \"qcalc\"\r\n    type(argparser) :: args\r\n    args = argparser(\"A quantum physics calculation program.\")\r\n    call args%set_program_name(program_name)\r\n    call args%add_help_option()\r\n    call args%add_sc_option(\"-v\", \"--version\", \"show version info\", show_version_info)\r\n    call args%add_option_logical(\"-o\", \"--openmp\", \"use openmp or not\") ! logical option has no default\r\n    call args%add_option_logical(\"-m\", \"--mpi\", \"use mpi or not\")\r\n    call args%add_option_integer(\"-t\", \"--thread\", \"thread number,\\nit is valid only if openmp is set\", 1)\r\n    call args%add_option_integer(\"-p\", \"--process\", \"process number,\\nit is valid only if mpi is set\", 1)\r\n    call args%add_option_string(\"\", \"--chemical\", \"chemical formula\", \"H2O\") ! short name can be empty\r\n    call args%add_named_argument_string(\"input\", \"initialize file\")\r\n    call args%add_named_argument_string(\"output\", \"output file\")\r\n    call args%parse()\r\n\r\n    if (args%has_option(\"--openmp\")) then\r\n        print '(A,I2,A)', \"openmp is used, and we use \", args%get_option_integer(\"-t\"), \" threads\"\r\n    end if\r\n    if (args%has_option(\"--mpi\")) then\r\n        print '(A,I2,A)', \"mpi is used, and we use \", args%get_option_integer(\"-p\"), \" processes\"\r\n    end if\r\n    print '(A,A)', \"the calculated chemical is \", trim(args%get_option_string(\"--chemical\"))\r\n    print '(A,A)', \"the input file is \", trim(args%get_argument_string(\"input\"))\r\n    print '(A,A)', \"the output file is \", trim(args%get_argument_string(\"output\"))\r\n\r\n    ! you can also print the cached `args` into INI file\r\n    ! print '(/,A)', \"All of the options and arguments are shown below\"\r\n    ! call args%print_as_ini(stdout, .true.)\r\ncontains\r\n    subroutine show_version_info\r\n        print \"(A)\", trim(program_name)//\" version 0.1.0\"\r\n    end subroutine show_version_info\r\nend program test\r\n```\r\nCompile it as `qclac`, and use it like:\r\n```bash\r\n\u003e qclac -?\r\nusage: qcalc [options] [=input] [=output]\r\n\r\nA quantum physics calculation program.\r\n\r\noptions:\r\n  -?, --help               show this help message\r\n  -v, --version            show version info\r\n  -o, --openmp             use openmp or not\r\n  -m, --mpi                use mpi or not\r\n  -t, --thread             (integer [=1]) thread number,\r\n                           it is valid only if openmp is set\r\n  -p, --process            (integer [=1]) process number,\r\n                           it is valid only if mpi is set\r\n  --chemical               (string [=H2O]) chemical formula\r\n\r\nnamed arguments:\r\n  input                    (string) initialize file\r\n  output                   (string) output file\r\n\u003e qclac -om -t 16 input=input.txt output=out.bin\r\nopenmp is used, and we use 16 threads\r\nmpi is used, and we use  1 processes\r\nthe calculated chemical is H2O\r\nthe input file is input.txt\r\nthe output file is out.bin\r\n```\r\n\r\n## Usage\r\n\r\nThe simple way to use this package is copy the file `src/argparse-f.f90` into your project. Or you can use [fpm](https://fpm.fortran-lang.org/en/index.html)\r\n```toml\r\n[dependencies]\r\nargparse-f = { git=\"https://github.com/0382/argparse-f.git\" }\r\n```\r\n\r\nIn addition, `argparse-f` also supports the [Meson](https://mesonbuild.com/) build system.\r\n\r\n## Parse rules\r\n\r\nIn this package, command line arguments are classified into two kinds: `option` and `argument`.\r\n\r\n### `option`\r\n\r\nAs the name suggests, `option` is optional. It cantains two types: normal option and short curcuit option.\r\n\r\n#### normal options\r\n\r\nYou can add normal options like this\r\n```fortran\r\ncall args%add_option_integer(\"-t\", \"--thread\", \"thread number\", 1)\r\n```\r\n\r\nThe first dummy argument means `short_name` of the option, and it can be empty. If it is not empty, then it must be `-` followed by **single character**. The second dummy argument means `long_name` of the option, and it **cannot** be empty, it must start with `--`. The third dummy argument means help message, and the last one is the default value of the option.\r\n\r\nIn this version, normal option support five data types: `logical, integer, real, real(kind=8), character(len=*)` (add `real(8)` option method is `add_option_double`, and add `character(len=*)` option method is `add_option_string`).\r\n\r\n`add_option_logical` function does not need the default value. Because, if you set the option in command line, the value is `.true.`, otherwise the value is `.false.`, for example:\r\n```bash\r\nls -l\r\n```\r\nthem `-l` option is set to `.true.`. To add the other four data types' options, you must give the default value, in case if one do not set the option, it use the default value. In command line, you should set the option like this:\r\n```bash\r\ngreet --name Alice --age 24\r\n```\r\nthen the `--name` option is set to `Alice` and `--age` option is set to `24`.\r\n\r\n#### short circuit options\r\n\r\nYou can add short circuit options like this:\r\n```fortran\r\ncall args%add_sc_option(\"-v\", \"--version\", \"show version info\", show_version_info)\r\ncontains\r\nsubroutine show_version_info\r\n    print \"(A)\", trim(program_name)//\" version 0.1.0\"\r\nend subroutine show_version_info\r\n```\r\n\r\nA short circuit option must be `logical` type, and you should give a callback subroutine. The callback subroutine cannot have dummy arguments. Short circuit options are searched first, for example\r\n```bash\r\ngit --help\r\ngcc -v\r\n```\r\nThe corresponding callback subroutine is called immediately as long as the program searched the first short circuit option, and then the program `stop`.\r\n\r\n### argument\r\n\r\n`argument` is opposited to `option`. You must set its value in command line. If not, the program will stop with error. `argument` also contains two types: position argument and named argument.\r\n\r\nIn this version, `argument` supports `integer, real, real(kind=8), character(len=*)` data types. It does not support `logical` type, you should use `option` instead.\r\n#### position argument\r\n\r\nPosition argument is got with position, for exmple\r\n```bash\r\nffplay video.mp4\r\n```\r\nThe `video.mp4` is the first (and only) position argument. If you do not give the argument, the `ffplay` will exit with error.\r\n\r\nAdd position argument like this:\r\n```fortran\r\ncall args%add_argument_string(\"input\", \"initialize file\")\r\n```\r\nThe first dummy argument is `name` of the argumet, and the second is the help message.\r\n\r\n#### named argument\r\n\r\nThe named argument is defined by myself, it is designed for my work. It is used like this:\r\n```bash\r\ngreet name=Alice age=24\r\n```\r\nIt is tedious comparing with position argument, so it should not be used in a common command line program. But it is useful in a big project, and you run program with a shell script. In this case, the named argument make the script more readable.\r\n\r\nAdd named argument like this:\r\n```fortran\r\ncall args%add_named_argument_string(\"input\", \"initialize file\")\r\n```\r\n\r\n## Get results\r\n\r\n### option\r\n\r\nUse functions like `args%get_option_logical(name)` to get option results. You can use both `short_name` and `long_name`. For `logical` data type, you can also use `args%has_option(name)` for short.\r\n\r\n### argument\r\n\r\nUse functions like `args%get_argument_logical(name)` to get argument results. Named argument and position argument cannot have duplicate name. So you can use this function to get either named argument or position argument.\r\n\r\n## Tips\r\n\r\n### conflict\r\n\r\nOptions cannot have same `short_name` or `long_name`, including normal options and short circuit options. Arguments also cannot have same `name`, including named arguments and position arguments.\r\n\r\n### aggregate short name options\r\nIn some linux command line programs, there are options I call them aggregate short name options. For example\r\n```bash\r\nls -lah\r\n```\r\nIt set three logical options `-l`, `-a`, `-h` at the same time. This package also support this feature. Remind that only `logical` type options are supported to be set in this way.\r\n\r\n\u003e That's why options' `short_name` only contians single character.\r\n\r\n### custom help option\r\n\r\nYou can add default help option with `args%add_help_option`, it's `short_name` and `long_name` are `-?` and `--help`. If you dislike the names, you can add custom help option like this\r\n```fortran\r\ncall args%add_sc_option(\"-h\", \"--help\", \"show this help message\", print_help)\r\ncontains\r\nsubroutine print_help\r\n    call args%print_help()\r\nend subroutine\r\n```\r\n\r\n\u003e Using `-?` is to save character space for other options' `short_name`. Not every one like it. Luckily it is simple to define your custom help option.\r\n\r\n### multi-line help message\r\nSome times, help message may be very long. You can use `\\n` to as line break marker. Of course, Fortran does not have escape characters. I just use some character split technique to realize this feature.\r\n\r\n### print argparser\r\n\r\nYou can print the argparser into INI file format. If the second dummy argument is set to `.true.`, then print help message as comments.\r\n```fortran\r\ncall args%print_as_ini(stdout, .true.)\r\n```\r\n\r\n### `print_uasge` \u0026\u0026 `set_program_name`\r\nIf you give none command line argument and if it needs at least one argument, the program will call `print_usage` and exit. It is just the first line of `print_help`. `set_program_name` only affects program name in `print_usage`, if you does not set it, it will use `argv[0]`.\r\n\r\n## Reference\r\n\r\nThis package works like my c++ package [argparse](https://github.com/0382/util/tree/main/cpp/argparse). They are imspired by c++ package [cmdline](https://github.com/tanakh/cmdline) and python's standard library package [argparse](https://docs.python.org/3/library/argparse.html).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0382%2Fargparse-f","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0382%2Fargparse-f","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0382%2Fargparse-f/lists"}