200 type(OPT_ENTRY),
pointer :: opt_table(:) => null()
203 logical :: initialized = .false.
204 type(cmd_opts_internal),
pointer :: cmd_opts_list(:) => null()
211 character(STRING),
pointer:: options(:) => null()
213 character(STRING) :: help_message
215 logical :: optvalue_flag
219 type cmd_opts_internal
220 character(STRING) :: name
221 character(STRING) ::
value
222 logical:: flag_called = .false.
225 end type cmd_opts_internal
228 module procedure dcargsopen0
232 module procedure dcargsclose0
236 module procedure dcargsoption0
240 module procedure dcargsputline0
244 module procedure dcargsdebug0
248 module procedure dcargshelp0
252 module procedure dcargshelpmsg0
256 module procedure dcargsstrict0
260 module procedure dcargsget0
264 module procedure dcargsnumber0
271 module procedure dcargsopen0
275 module procedure dcargsclose0
279 module procedure dcargsoption0
283 module procedure dcargsputline0
287 module procedure dcargsdebug0
291 module procedure dcargshelp0
295 module procedure dcargshelpmsg0
299 module procedure dcargsstrict0
303 module procedure dcargsget0
307 module procedure dcargsnumber0
313 character(STRING),
allocatable,
save:: argstr_table(:)
318 integer,
save:: argind_count = -1
324 type(CMD_OPTS_INTERNAL),
allocatable,
save :: cmd_opts_list(:)
329 character(STRING),
allocatable,
save:: cmd_argv_list(:)
336 subroutine dcargsopen0(arg)
347 use dc_message,
only: messagenotify
350 type(
args),
intent(out) :: arg
351 integer:: cmd_opts_max
352 character(len = *),
parameter :: subname =
'DCArgsOpen'
354 if (arg % initialized)
then
355 call messagenotify(
'W', subname,
'This argument (type ARGS) is already opend.')
360 cmd_opts_max =
size(cmd_opts_list)
361 allocate(arg % cmd_opts_list(cmd_opts_max))
362 arg % cmd_opts_list = cmd_opts_list
363 nullify( arg % opt_table )
364 arg % initialized = .true.
365 end subroutine dcargsopen0
367 subroutine dcargsclose0(arg)
373 type(
args),
intent(inout) :: arg
376 if (arg % initialized)
then
377 if (
associated( arg % opt_table ) )
then
378 do i = 1,
size(arg % opt_table)
379 deallocate(arg % opt_table(i) % options)
382 deallocate(arg % opt_table)
385 deallocate(arg % cmd_opts_list)
386 deallocate(argstr_table)
387 deallocate(cmd_argv_list)
388 deallocate(cmd_opts_list)
392 end subroutine dcargsclose0
394 subroutine dcargsoption0(arg, options, flag, value, help)
433 use dc_message,
only: messagenotify
435 type(
args),
intent(inout) :: arg
436 character(len = *),
intent(in) :: options(:)
437 logical,
intent(out) :: flag
438 character(len = *),
intent(out),
optional :: value
439 character(len = *),
intent(in),
optional :: help
440 integer :: i, j, options_size, table_size
441 type(opt_entry),
allocatable :: local_tables(:)
442 character(len = STRING) :: opt_name, opt_value, opt_full
443 character(len = *),
parameter :: subname =
'DCArgsOption'
446 if (
present(
value))
value =
''
447 if (.not. arg % initialized)
then
448 call messagenotify(
'W', subname,
'Call Open before Option in dc_args.')
451 options_size =
size(options)
452 if (options_size < 1)
then
460 if ( .not.
associated( arg % opt_table ) )
then
464 allocate(arg % opt_table(table_size + 1))
468 table_size =
size(arg % opt_table)
469 allocate(local_tables(table_size))
470 local_tables(1:table_size) = arg % opt_table(1:table_size)
471 deallocate(arg % opt_table)
472 allocate(arg % opt_table(table_size + 1))
473 arg % opt_table(1:table_size) = local_tables(1:table_size)
474 deallocate(local_tables)
478 allocate(arg % opt_table(table_size + 1) % options(options_size))
479 arg % opt_table(table_size + 1) % options = options
480 arg % opt_table(table_size + 1) % help_message =
''
481 if (
present(
help))
then
482 arg % opt_table(table_size + 1) % help_message =
help
484 arg % opt_table(table_size + 1) % optvalue_flag =
present(
value)
488 do i = 1, options_size
489 opt_full = arg % opt_table(table_size + 1) % options(i)
490 if (dcoptionformc(opt_full, opt_name, opt_value))
then
491 arg % opt_table(table_size + 1) % options(i) = opt_name
493 if (len(trim(adjustl(opt_full))) < 2)
then
494 arg % opt_table(table_size + 1) % options(i) = &
495 &
'-' // trim(adjustl(opt_full))
497 arg % opt_table(table_size + 1) % options(i) = &
498 &
'--' // trim(adjustl(opt_full))
506 do i = 1, options_size
507 do j = 1,
size(arg % cmd_opts_list)
508 if (trim(arg % opt_table(table_size + 1) % options(i)) &
509 & == trim(arg % cmd_opts_list(j) % name))
then
511 if (
present(
value))
then
512 value = arg % cmd_opts_list(j) % value
514 arg % cmd_opts_list(j) % flag_called = .true.
518 end subroutine dcargsoption0
520 subroutine dcargsdebug0(arg)
528 use dc_string,
only: stoa, stoi
529 use dc_trace,
only: setdebug
530 use dc_message,
only: messagenotify
532 type(
args),
intent(inout) :: arg
534 character(STRING) :: VAL_debug
535 character(len = *),
parameter :: subname =
'DCArgsDebug'
537 if (.not. arg % initialized)
then
538 call messagenotify(
'W', subname,
'Call Open before Debug in dc_args.')
541 call option(arg, stoa(
'-D',
'--debug'), opt_debug, val_debug, &
542 &
help=
"call dc_trace#SetDebug (display a lot of messages for debug). " // &
543 &
"VAL is unit number (default is standard output)")
545 if (trim(val_debug) ==
'')
then
548 call setdebug(stoi(val_debug))
552 end subroutine dcargsdebug0
555 subroutine dcargshelp0(arg, force)
572 use dc_string,
only: stoa, stoi, printf, concat, joinchar, uchar, lchar
573 use dc_present,
only: present_and_true
574 use dc_message,
only: messagenotify
577 type(
args),
intent(inout) :: arg
578 logical,
intent(in),
optional :: force
579 logical :: OPT_help, found, end
580 character(STRING) :: VAL_help, options_msg, help_msg, category
581 character(STRING),
pointer :: localopts(:) => null()
583 character(len = *),
parameter :: subname =
'DCArgsHelp'
585 if (.not. arg % initialized)
then
586 call messagenotify(
'W', subname,
'Call Open before Help in dc_args.')
589 call dcargsoption(arg, stoa(
'-h',
'-H',
'--help'), opt_help, val_help, &
590 &
help=
"display this help and exit. " // &
591 &
"VAL is unit number (default is standard output)")
592 if (.not. opt_help .and. .not. present_and_true(force))
then
595 if (trim(val_help) ==
'')
then
598 unit = stoi(val_help)
601 call printf(unit,
'')
603 call dchashget(arg % helpmsg,
'TITLE', help_msg, found)
605 call printf(unit,
'%c', c1=trim(help_msg))
606 call printf(unit,
'')
610 call dchashget(arg % helpmsg,
'OVERVIEW', help_msg, found)
612 call printf(unit,
'Overview::')
613 call printautolinefeed(unit, help_msg, indent=
' ')
614 call printf(unit,
'')
618 call dchashget(arg % helpmsg,
'USAGE', help_msg, found)
620 call printf(unit,
'Usage::')
621 call printautolinefeed(unit, help_msg, indent=
' ')
622 call printf(unit,
'')
626 call printf(unit,
'Options::')
627 if (
associated(arg % opt_table) )
then
628 do i = 1,
size(arg % opt_table)
630 if (arg % opt_table(i) % optvalue_flag)
then
631 call concat(arg % opt_table(i) % options,
'=VAL', localopts)
633 allocate(localopts(
size(arg % opt_table(i) % options)))
634 localopts = arg % opt_table(i) % options
636 options_msg = trim(options_msg) // trim(joinchar(localopts))
637 deallocate(localopts)
638 call printf(unit,
' %c', c1=trim(options_msg))
639 call printautolinefeed(unit, &
640 & arg % opt_table(i) % help_message, indent=
' ')
641 call printf(unit,
'')
647 call dchashnext(arg % helpmsg, category, help_msg,
end)
650 call printf(unit,
'%c%c::', &
651 & c1=trim(uchar(category(1:1))), c2=trim(lchar(category(2:))))
652 call printautolinefeed(unit, help_msg, indent=
' ')
653 call printf(unit,
'')
660 end subroutine dcargshelp0
662 subroutine dcargshelpmsg0(arg, category, msg)
729 use dc_string,
only: uchar
730 use dc_message,
only: messagenotify
732 type(
args),
intent(inout) :: arg
733 character(*),
intent(in) :: category
734 character(*),
intent(in) :: msg
735 character(len = *),
parameter :: subname =
'DCArgsHelpMsg'
737 if (.not. arg % initialized)
then
738 call messagenotify(
'W', subname,
'Call Open before Help in dc_args.')
741 call dchashput(arg % helpmsg, key=uchar(category),
value=msg)
742 end subroutine dcargshelpmsg0
745 subroutine dcargsstrict0(arg, severe)
762 use dc_present,
only: present_and_true
763 use dc_message,
only: messagenotify
765 type(
args),
intent(inout) :: arg
766 logical,
intent(in),
optional :: severe
767 character(STRING) :: err_mess
769 character(len = *),
parameter :: subname =
'DCArgsStrict'
771 if (.not. arg % initialized)
then
772 call messagenotify(
'W', subname,
'Call Open before Help in dc_args.')
775 do i = 1,
size(arg % cmd_opts_list)
776 err_mess = trim(arg % cmd_opts_list(i) % name) //
' is invalid option.'
777 if (.not. arg % cmd_opts_list(i) % flag_called)
then
778 if (present_and_true(severe))
then
779 call messagenotify(
'E', subname, err_mess)
781 call messagenotify(
'W', subname, err_mess)
785 end subroutine dcargsstrict0
788 subroutine dcargsget0(arg, argv)
797 use dc_string,
only: stoa, stoi, printf, concat, joinchar
798 use dc_present,
only: present_and_true
799 use dc_message,
only: messagenotify
801 type(
args),
intent(inout) :: arg
802 character(*),
pointer :: argv(:)
803 integer :: i, cmd_argv_max
804 character(len = *),
parameter :: subname =
'DCArgsGet'
806 if (.not. arg % initialized)
then
807 call messagenotify(
'W', subname,
'Call Open before Help in dc_args.')
810 cmd_argv_max =
size(cmd_argv_list)
811 allocate(argv(cmd_argv_max))
812 do i = 1, cmd_argv_max
813 argv(i) = cmd_argv_list(i)
815 end subroutine dcargsget0
817 function dcargsnumber0(arg)
result(result)
821 use dc_message,
only: messagenotify
823 type(
args),
intent(inout) :: arg
825 character(len = *),
parameter :: subname =
'DCArgsNumber'
827 if (.not. arg % initialized)
then
828 call messagenotify(
'W', subname,
'Call Open before Help in dc_args.')
831 result =
size(cmd_argv_list)
832 end function dcargsnumber0
834 subroutine dcargsputline0(arg)
839 use dc_string,
only: printf, joinchar
841 type(
args),
intent(in) :: arg
844 if (.not. arg % initialized)
then
845 call printf(
stdout,
'#<ARGS:: @initialized=%y>', l=(/arg % initialized/))
848 call printf(
stdout,
'#<ARGS:: @initialized=%y,', l=(/arg % initialized/))
849 call printf(
stdout,
' @opt_table(:)=')
850 if (
associated(arg % opt_table) )
then
851 do i = 1,
size(arg % opt_table)
852 call printf(
stdout,
' #<OPT_ENTRY:: ')
853 call printf(
stdout,
' @options=%c, @help_message=%c, @optvalue_flag=%y', &
854 & c1=trim(joinchar(arg % opt_table(i) % options)), &
855 & c2=trim(arg % opt_table(i) % help_message), &
856 & l=(/arg % opt_table(i) % optvalue_flag/))
861 call printf(
stdout,
' @cmd_opts_list(:)=')
862 do i = 1,
size(arg % cmd_opts_list)
863 call printf(
stdout,
' #<CMD_OPTS_INTERNAL:: ')
864 call printf(
stdout,
' @name=%c, @value=%c, @flag_called=%y', &
865 & c1=trim(arg % cmd_opts_list(i) % name), &
866 & c2=trim(arg % cmd_opts_list(i) % value), &
867 & l=(/arg % cmd_opts_list(i) % flag_called/))
871 call printf(
stdout,
' @cmd_argv_list(:)=%c', &
872 & c1=trim(joinchar(cmd_argv_list)))
875 end subroutine dcargsputline0
877 subroutine printautolinefeed(unit, fmt, length, indent)
889 use dc_string,
only: split
891 character(*),
intent(in) :: fmt
892 integer,
intent(in),
optional :: length
893 character(*),
intent(in),
optional :: indent
894 integer,
intent(in),
optional :: unit
895 character(STRING),
pointer :: carray_tmp(:) => null()
896 character(STRING) :: store_str
897 integer,
parameter :: default_len = 70
898 integer :: i, split_len, indent_len, unit_num
899 logical :: new_line_flag
901 if (
present(unit))
then
907 if (
present(indent))
then
908 indent_len = len(indent)
913 if (
present(length))
then
914 split_len = length - indent_len
916 split_len = default_len - indent_len
921 call split(fmt, carray_tmp,
'')
923 new_line_flag = .true.
926 if (i >
size(carray_tmp))
then
927 write(unit_num,
'(A)') trim(store_str)
931 if (len(trim(store_str)) + len(trim(carray_tmp(i))) > split_len)
then
932 if (new_line_flag)
then
933 write(unit_num,
'(A)') trim(carray_tmp(i))
936 write(unit_num,
'(A)') trim(store_str)
938 new_line_flag = .true.
943 if (new_line_flag .and.
present(indent))
then
944 store_str = indent // trim(carray_tmp(i))
946 store_str = trim(store_str) //
' ' // trim(carray_tmp(i))
948 new_line_flag = .false.
952 end subroutine printautolinefeed
954 subroutine sortargtable
965 character(STRING):: raw_arg, name, value
966 integer:: i, cmd_argv_count, cmd_opts_count, cmd_argv_max, cmd_opts_max
968 if (
allocated(cmd_opts_list))
return
971 check_count:
do, i = 1, argind_count
972 raw_arg = argstr_table(i)
973 if (dcoptionformc(raw_arg, name,
value))
then
974 cmd_opts_count = cmd_opts_count + 1
976 cmd_argv_count = cmd_argv_count + 1
980 cmd_argv_max = cmd_argv_count
981 cmd_opts_max = cmd_opts_count
983 allocate(cmd_argv_list(cmd_argv_max))
984 allocate(cmd_opts_list(cmd_opts_max))
988 arg_get :
do, i = 1, argind_count
989 raw_arg = argstr_table(i)
990 if (dcoptionformc(raw_arg, name,
value))
then
991 cmd_opts_count = cmd_opts_count + 1
992 cmd_opts_list(cmd_opts_count) % name = name
993 cmd_opts_list(cmd_opts_count) % value =
value
994 cmd_opts_list(cmd_opts_count) % flag_called = .false.
996 cmd_argv_count = cmd_argv_count + 1
997 cmd_argv_list(cmd_argv_count) = raw_arg
1000 end subroutine sortargtable
1002 subroutine buildargtable
1014 integer:: i, narg, nargmax
1015 character(len = STRING):: value
1016 character(len = STRING),
allocatable:: localtab(:)
1018 if (argind_count >= 0)
return
1020 allocate(localtab(nargmax))
1025 localtab(narg) =
value
1028 allocate(argstr_table(narg))
1029 argstr_table(1: narg) = localtab(1: narg)
1030 deallocate(localtab)
1031 end subroutine buildargtable
1033 function dcoptionformc(argument, name, value)
result(result)
1065 character(len = *),
intent(in):: argument
1066 character(len = *),
intent(out):: name, value
1070 equal = index(argument,
'=')
1071 if (argument(1:1) ==
'-' .and. argument(2:2) /=
'-')
then
1073 if (equal == 0)
then
1074 name = argument(1:2)
1077 name = argument(1:2)
1078 value = argument(equal+1: )
1081 elseif (argument(1:2) ==
'--')
then
1083 if (equal == 0)
then
1087 name = argument(1:equal-1)
1088 value = argument(equal+1: )
1103 end function dcoptionformc
Provides kind type parameter values.
integer, parameter, public string
Character length for string
integer, parameter, public stdout
Unit number for Standard OUTPUT
subroutine, public sysdepargget(index, val)
integer function, public sysdepargcount()