Skip to content

Commit

Permalink
fix #9, go with no cmd line args if no (named)argument is needed
Browse files Browse the repository at this point in the history
  • Loading branch information
0382 committed Apr 10, 2024
1 parent ab2e4c8 commit 4373d34
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ In this version, normal option support five data types: `logical, integer, real,
```bash
ls -l
```
them `-l` option is set to `.true.`. To add the other four data type's options, you must give the default value, in case if one do not set the option, we use the default value. In command line, you should set the option like this:
them `-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:
```bash
greet --name Alice --age 24
```
Expand All @@ -130,7 +130,7 @@ The corresponding callback subroutine is called immediately as long as the progr
### argument
`argument` is opposited to `option`. You must set it's value in command line. If not, the program will stop with error. `argument` also contains two types: position argument and named argument.
`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.
In this version, `argument` supports `integer, real, real(kind=8), character(len=*)` data types. It does not support `logical` type, you should use `option` instead.
#### position argument
Expand All @@ -153,7 +153,7 @@ The named argument is defined by myself, it is designed for my work. It is used
```bash
greet name=Alice age=24
```
It is tedious comparing with position argument, so it should not used in a common command line program. But it useful in a big project, and you run program with a shell script. In this case, the named argument make the script more readable.
It 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.
Add named argument like this:
```fortran
Expand Down Expand Up @@ -209,7 +209,7 @@ call args%print_as_ini(stdout, .true.)
```
### `print_uasge` && `set_program_name`
If you give none command line 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]`.
If 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]`.
## Reference
Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ call args%print_as_ini(stdout, .true.)
这个函数的第二个参数表示是否打印注释,如果为`.true.`那么会把帮助信息打印为注释。

### `print_uasge`和`set_program_name`
如果你运行程序不带任何命令行参数,那么`argparser`会调用`print_usage`并退出。`print_usage`实际上就是`print_help`的第一行信息,算是一个简短的帮助信息。`set_program_name`仅仅影响`print_usage`时显示的程序名字,如果不调用这个函数,那么会使用`argv[0]`
如果你运行程序不带任何命令行参数同时程序应该需要argument,那么`argparser`会调用`print_usage`并退出。`print_usage`实际上就是`print_help`的第一行信息,算是一个简短的帮助信息。`set_program_name`仅仅影响`print_usage`时显示的程序名字,如果不调用这个函数,那么会使用`argv[0]`

## 参考

Expand Down
4 changes: 2 additions & 2 deletions src/argparse-f.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
! MIT License

! Copyright (c) 2023 0382
! Copyright (c) 2023 0382 and argparse-f's contributors

! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -156,7 +156,7 @@ subroutine argp_parse(this)
print "(A)", "WARNING: you get a truncated program name"
end if
end if
if (argc == 0) then
if (argc == 0 .and. (this%argument_size /= 0 .or. this%named_argument_size /= 0)) then
call this%print_usage()
stop
end if
Expand Down

0 comments on commit 4373d34

Please sign in to comment.