-
Notifications
You must be signed in to change notification settings - Fork 4
/
M_CLI2.F90
6241 lines (6041 loc) · 266 KB
/
M_CLI2.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!VERSION 1.0 20200115
!VERSION 2.0 20200802
!VERSION 3.0 20201021 LONG:SHORT syntax
!VERSION 3.1 20201115 LONG:SHORT:: syntax
!===================================================================================================================================
!()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()!
!===================================================================================================================================
!>
!!##NAME
!! M_CLI2(3fm) - [ARGUMENTS::M_CLI2::INTRO] command line argument
!! parsing using a prototype command
!! (LICENSE:PD)
!!##SYNOPSIS
!!
!! Available procedures and variables:
!!
!! use M_CLI2, only : set_args, get_args, unnamed, remaining, args
!! use M_CLI2, only : get_args_fixed_length, get_args_fixed_size
!! use M_CLI2, only : specified
!! ! convenience functions
!! use M_CLI2, only : dget, iget, lget, rget, sget, cget
!! use M_CLI2, only : dgets, igets, lgets, rgets, sgets, cgets
!!
!!##DESCRIPTION
!! Allow for command line parsing much like standard Unix command line
!! parsing using a simple prototype.
!!
!! Typically one call to SET_ARGS(3f) is made to define the command
!! arguments, set default values and parse the command line. Then a call
!! is made to the convenience commands based on GET_ARGS(3f) for each
!! command keyword to obtain the argument values.
!!
!! The documentation for SET_ARGS(3f) and GET_ARGS(3f) provides further
!! details.
!!
!!##EXAMPLE
!!
!!
!! Sample typical minimal usage
!!
!! program minimal
!! use M_CLI2, only : set_args, lget, rget, filenames=>unnamed
!! implicit none
!! real :: x, y
!! integer :: i
!! call set_args(' -y 0.0 -x 0.0 -v F')
!! x=rget('x')
!! y=rget('y')
!! if(lget('v'))then
!! write(*,*)'X=',x
!! write(*,*)'Y=',y
!! write(*,*)'ATAN2(Y,X)=',atan2(x=x,y=y)
!! else
!! write(*,*)atan2(x=x,y=y)
!! endif
!! if(size(filenames).gt.0)then
!! write(*,'(g0)')'filenames:'
!! write(*,'(i6.6,3a)')(i,'[',filenames(i),']',i=1,size(filenames))
!! endif
!! end program minimal
!!
!! Sample program using type get_args() and variants
!!
!! program demo_M_CLI2
!! use M_CLI2, only : set_args, get_args
!! use M_CLI2, only : filenames=>unnamed
!! use M_CLI2, only : get_args_fixed_length, get_args_fixed_size
!! implicit none
!! integer :: i
!! integer,parameter :: dp=kind(0.0d0)
!! !
!! ! DEFINE ARGS
!! real :: x, y, z
!! real(kind=dp),allocatable :: point(:)
!! logical :: l, lbig
!! logical,allocatable :: logicals(:)
!! character(len=:),allocatable :: title ! VARIABLE LENGTH
!! character(len=40) :: label ! FIXED LENGTH
!! real :: p(3) ! FIXED SIZE
!! logical :: logi(3) ! FIXED SIZE
!! !
!! ! DEFINE AND PARSE (TO SET INITIAL VALUES) COMMAND LINE
!! ! o set a value for all keywords.
!! ! o double-quote strings
!! ! o set all logical values to F or T.
!! ! o value delimiter is comma, colon, or space
!! call set_args(' &
!! & -x 1 -y 2 -z 3 &
!! & -p -1 -2 -3 &
!! & --point 11.11, 22.22, 33.33e0 &
!! & --title "my title" -l F -L F &
!! & --logicals F F F F F &
!! & -logi F T F &
!! & --label " " &
!! ! note space between quotes is required
!! & ')
!! ! ASSIGN VALUES TO ELEMENTS
!! call get_args('x',x) ! SCALARS
!! call get_args('y',y)
!! call get_args('z',z)
!! call get_args('l',l)
!! call get_args('L',lbig)
!! call get_args('title',title) ! ALLOCATABLE STRING
!! call get_args('point',point) ! ALLOCATABLE ARRAYS
!! call get_args('logicals',logicals)
!! !
!! ! for NON-ALLOCATABLE VARIABLES
!!
!! ! for non-allocatable string
!! call get_args_fixed_length('label',label)
!!
!! ! for non-allocatable arrays
!! call get_args_fixed_size('p',p)
!! call get_args_fixed_size('logi',logi)
!! !
!! ! USE VALUES
!! write(*,*)'x=',x, 'y=',y, 'z=',z, x+y+z
!! write(*,*)'p=',p
!! write(*,*)'point=',point
!! write(*,*)'title=',title
!! write(*,*)'label=',label
!! write(*,*)'l=',l
!! write(*,*)'L=',lbig
!! write(*,*)'logicals=',logicals
!! write(*,*)'logi=',logi
!! !
!! ! unnamed strings
!! !
!! if(size(filenames).gt.0)then
!! write(*,'(i6.6,3a)')(i,'[',filenames(i),']',i=1,size(filenames))
!! endif
!! !
!! end program demo_M_CLI2
!!
!!##AUTHOR
!! John S. Urban, 2019
!!##LICENSE
!! Public Domain
!===================================================================================================================================
module M_CLI2
use, intrinsic :: iso_fortran_env, only : stderr=>ERROR_UNIT, stdin=>INPUT_UNIT, stdout=>OUTPUT_UNIT, warn=>OUTPUT_UNIT
! copied to M_CLI2 for a stand-alone version
!use M_strings, only : upper, lower, quote, replace_str=>replace, unquote, split, string_to_value, atleast
!use M_list, only : insert, locate, remove, replace
!use M_args, only : longest_command_argument
!use M_journal, only : journal
implicit none
integer,parameter,private :: dp=kind(0.0d0)
integer,parameter,private :: sp=kind(0.0)
private
!logical,save :: debug_m_cli2=.true.
logical,public,save :: debug_m_cli2=.false.
!===================================================================================================================================
character(len=*),parameter :: gen='(*(g0))'
character(len=:),allocatable,public :: unnamed(:)
character(len=:),allocatable,public :: args(:)
character(len=:),allocatable,public :: remaining
public :: set_args
public :: get_subcommand
public :: get_args
public :: get_args_fixed_size
public :: get_args_fixed_length
public :: specified
public :: print_dictionary
public :: dget, iget, lget, rget, sget, cget
public :: dgets, igets, lgets, rgets, sgets, cgets
public :: CLI_RESPONSE_FILE
private :: check_commandline
private :: wipe_dictionary
private :: prototype_to_dictionary
private :: update
private :: prototype_and_cmd_args_to_nlist
private :: get
type option
character(:),allocatable :: shortname
character(:),allocatable :: longname
character(:),allocatable :: value
integer :: length
logical :: present_in
logical :: mandatory
end type option
!===================================================================================================================================
character(len=:),allocatable,save :: keywords(:)
character(len=:),allocatable,save :: shorts(:)
character(len=:),allocatable,save :: values(:)
integer,allocatable,save :: counts(:)
logical,allocatable,save :: present_in(:)
logical,allocatable,save :: mandatory(:)
logical,save :: G_keyword_single_letter=.true.
character(len=:),allocatable,save :: G_passed_in
logical,save :: G_remaining_on, G_remaining_option_allowed
character(len=:),allocatable,save :: G_remaining
character(len=:),allocatable,save :: G_subcommand ! possible candidate for a subcommand
character(len=:),allocatable,save :: G_STOP_MESSAGE
integer,save :: G_STOP
logical,save :: G_QUIET
logical,save :: G_STRICT ! strict short and long rules or allow -longname and --shortname
character(len=:),allocatable,save :: G_PREFIX
!----------------------------------------------
! try out response files
logical,save :: CLI_RESPONSE_FILE=.false. ! allow @name abbreviations
logical,save :: G_APPEND ! whether to append or replace when duplicate keywords found
logical,save :: G_OPTIONS_ONLY ! process response file only looking for options for get_subcommand()
logical,save :: G_RESPONSE ! allow @name abbreviations
character(len=:),allocatable,save :: G_RESPONSE_IGNORED
!----------------------------------------------
!===================================================================================================================================
! return allocatable arrays
interface get_args; module procedure get_anyarray_d; end interface ! any size array
interface get_args; module procedure get_anyarray_i; end interface ! any size array
interface get_args; module procedure get_anyarray_r; end interface ! any size array
interface get_args; module procedure get_anyarray_x; end interface ! any size array
interface get_args; module procedure get_anyarray_c; end interface ! any size array and any length
interface get_args; module procedure get_anyarray_l; end interface ! any size array
! return scalars
interface get_args; module procedure get_scalar_d; end interface
interface get_args; module procedure get_scalar_i; end interface
interface get_args; module procedure get_scalar_real; end interface
interface get_args; module procedure get_scalar_complex; end interface
interface get_args; module procedure get_scalar_logical; end interface
interface get_args; module procedure get_scalar_anylength_c; end interface ! any length
! multiple scalars
interface get_args; module procedure many_args; end interface
!==================================================================================================================================
! return non-allocatable arrays
! said in conflict with get_args_*. Using class to get around that.
! that did not work either. Adding size parameter as optional parameter works; but using a different name
interface get_args_fixed_size; module procedure get_fixedarray_class; end interface ! any length, fixed size array
!interface get_args; module procedure get_fixedarray_d; end interface
!interface get_args; module procedure get_fixedarray_i; end interface
!interface get_args; module procedure get_fixedarray_r; end interface
!interface get_args; module procedure get_fixedarray_l; end interface
!interface get_args; module procedure get_fixedarray_fixed_length_c; end interface
interface get_args_fixed_length; module procedure get_args_fixed_length_a_array; end interface ! fixed length any size array
interface get_args_fixed_length; module procedure get_args_fixed_length_scalar_c; end interface ! fixed length
!===================================================================================================================================
!intrinsic findloc
!===================================================================================================================================
! ident_1="@(#) M_CLI2 str(3f) {msg_scalar msg_one}"
private str
interface str
module procedure msg_scalar, msg_one
end interface str
!===================================================================================================================================
private locate ! [M_CLI2] find PLACE in sorted character array where value can be found or should be placed
private locate_c
private insert ! [M_CLI2] insert entry into a sorted allocatable array at specified position
private insert_c
private insert_i
private insert_l
private replace ! [M_CLI2] replace entry by index from a sorted allocatable array if it is present
private replace_c
private replace_i
private replace_l
private remove ! [M_CLI2] delete entry by index from a sorted allocatable array if it is present
private remove_c
private remove_i
private remove_l
! Generic subroutine inserts element into allocatable array at specified position
interface locate; module procedure locate_c ; end interface
interface insert; module procedure insert_c, insert_i, insert_l ; end interface
interface replace; module procedure replace_c, replace_i, replace_l ; end interface
interface remove; module procedure remove_c, remove_i, remove_l ; end interface
!-----------------------------------------------------------------------------------------------------------------------------------
! convenience functions
interface cgets;module procedure cgs, cg;end interface
interface dgets;module procedure dgs, dg;end interface
interface igets;module procedure igs, ig;end interface
interface lgets;module procedure lgs, lg;end interface
interface rgets;module procedure rgs, rg;end interface
interface sgets;module procedure sgs, sg;end interface
!-----------------------------------------------------------------------------------------------------------------------------------
contains
!===================================================================================================================================
!()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()!
!===================================================================================================================================
!>
!!##NAME
!! check_commandline(3f) - [ARGUMENTS:M_CLI2]check command and process
!! pre-defined options
!!
!!##SYNOPSIS
!!
!! subroutine check_commandline(help_text,version_text,ierr,errmsg)
!!
!! character(len=*),intent(in),optional :: help_text(:)
!! character(len=*),intent(in),optional :: version_text(:)
!!
!!##DESCRIPTION
!! Checks the commandline and processes the implicit --help, --version,
!! --verbose, and --usage parameters.
!!
!! If the optional text values are supplied they will be displayed by
!! --help and --version command-line options, respectively.
!!
!!##OPTIONS
!!
!! HELP_TEXT if present, will be displayed if program is called with
!! --help switch, and then the program will terminate. If
!! not supplied, the command line initialized string will be
!! shown when --help is used on the commandline.
!!
!! VERSION_TEXT if present, will be displayed if program is called with
!! --version switch, and then the program will terminate.
!!
!! If the first four characters of each line are "@(#)" this prefix
!! will not be displayed and the last non-blank letter will be
!! removed from each line. This if for support of the SCCS what(1)
!! command. If you do not have the what(1) command on GNU/Linux and
!! Unix platforms you can probably see how it can be used to place
!! metadata in a binary by entering:
!!
!! strings demo_commandline|grep '@(#)'|tr '>' '\n'|sed -e 's/ */ /g'
!!
!!##EXAMPLE
!!
!!
!! Typical usage:
!!
!! program check_commandline
!! use M_CLI2, only : unnamed, set_args, get_args
!! implicit none
!! integer :: i
!! character(len=:),allocatable :: version_text(:), help_text(:)
!! real :: x, y, z
!! character(len=*),parameter :: cmd='-x 1 -y 2 -z 3'
!! version_text=[character(len=80) :: "version 1.0","author: me"]
!! help_text=[character(len=80) :: &
!! & "wish I put instructions","here","I suppose?"]
!! call set_args(cmd,help_text,version_text)
!! call get_args('x',x,'y',y,'z',z)
!! ! All done cracking the command line. Use the values in your program.
!! write (*,*)x,y,z
!! ! the optional unnamed values on the command line are
!! ! accumulated in the character array "UNNAMED"
!! if(size(unnamed).gt.0)then
!! write (*,'(a)')'files:'
!! write (*,'(i6.6,3a)') (i,'[',unnamed(i),']',i=1,size(unnamed))
!! endif
!! end program check_commandline
!===================================================================================================================================
subroutine check_commandline(help_text,version_text)
character(len=*),intent(in),optional :: help_text(:)
character(len=*),intent(in),optional :: version_text(:)
character(len=:),allocatable :: line
integer :: i
integer :: istart
integer :: iback
if(get('usage').eq.'T')then
call print_dictionary('USAGE:')
!x!call default_help()
call mystop(32)
return
endif
if(present(help_text))then
if(get('help').eq.'T')then
do i=1,size(help_text)
call journal('sc',help_text(i))
enddo
call mystop(1,'displayed help text')
return
endif
elseif(get('help').eq.'T')then
call default_help()
call mystop(2,'displayed default help text')
return
endif
if(present(version_text))then
if(get('version').eq.'T')then
istart=1
iback=0
if(size(version_text).gt.0)then
if(index(version_text(1),'@'//'(#)').eq.1)then ! allow for what(1) syntax
istart=5
iback=1
endif
endif
do i=1,size(version_text)
!xINTEL BUG*!call journal('sc',version_text(i)(istart:len_trim(version_text(i))-iback))
line=version_text(i)(istart:len_trim(version_text(i))-iback)
call journal('sc',line)
enddo
call mystop(3,'displayed version text')
return
endif
elseif(get('version').eq.'T')then
if(G_QUIET)then
G_STOP_MESSAGE = 'no version text'
else
call journal('sc','*check_commandline* no version text')
endif
call mystop(4,'displayed default version text')
return
endif
contains
subroutine default_help()
character(len=:),allocatable :: cmd_name
integer :: ilength
call get_command_argument(number=0,length=ilength)
if(allocated(cmd_name))deallocate(cmd_name)
allocate(character(len=ilength) :: cmd_name)
call get_command_argument(number=0,value=cmd_name)
G_passed_in=G_passed_in//repeat(' ',len(G_passed_in))
call substitute(G_passed_in,' --',NEW_LINE('A')//' --')
if(.not.G_QUIET)then
call journal('sc',cmd_name,G_passed_in) ! no help text, echo command and default options
endif
deallocate(cmd_name)
end subroutine default_help
end subroutine check_commandline
!===================================================================================================================================
!()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()!
!===================================================================================================================================
!>
!!##NAME
!! set_args(3f) - [ARGUMENTS:M_CLI2] command line argument parsing
!! (LICENSE:PD)
!!
!!##SYNOPSIS
!!
!! subroutine set_args(definition,help_text,version_text,ierr,errmsg)
!!
!! character(len=*),intent(in),optional :: definition
!! character(len=*),intent(in),optional :: help_text(:)
!! character(len=*),intent(in),optional :: version_text(:)
!! integer,intent(out),optional :: ierr
!! character(len=:),intent(out),allocatable,optional :: errmsg
!!##DESCRIPTION
!!
!! SET_ARGS(3f) requires a unix-like command prototype for defining
!! arguments and default command-line options. Argument values are then
!! read using GET_ARGS(3f).
!!
!! The --help and --version options require the optional
!! help_text and version_text values to be provided.
!!
!!##OPTIONS
!!
!! DEFINITION composed of all command arguments concatenated
!! into a Unix-like command prototype string. For
!! example:
!!
!! call set_args('-L F -ints 10,20,30 -title "my title" -R 10.3')
!!
!! DEFINITION is pre-defined to act as if started with
!! the reserved options '--verbose F --usage F --help
!! F --version F'. The --usage option is processed when
!! the set_args(3f) routine is called. The same is true
!! for --help and --version if the optional help_text
!! and version_text options are provided.
!!
!! see "DEFINING THE PROTOTYPE" in the next section for
!! further details.
!!
!! HELP_TEXT if present, will be displayed if program is called with
!! --help switch, and then the program will terminate. If
!! not supplied, the command line initialization string
!! will be shown when --help is used on the commandline.
!!
!! VERSION_TEXT if present, will be displayed if program is called with
!! --version switch, and then the program will terminate.
!! IERR if present a non-zero option is returned when an
!! error occurs instead of program execution being
!! terminated
!! ERRMSG a description of the error if ierr is present
!!
!!##DEFINING THE PROTOTYPE
!! o all keywords on the prototype MUST get a value.
!!
!! o logicals MUST be set to F or T.
!!
!! o strings MUST be delimited with double-quotes and
!! must be at least one space. Internal double-quotes
!! are represented with two double-quotes.
!!
!! o numeric keywords are not allowed; but this allows
!! negative numbers to be used as values.
!!
!! o lists of values should be comma-delimited unless a
!! user-specified delimiter is used. The prototype
!! must use the same array delimiters as the call to
!! the family of get_args*(3f) called.
!!
!! o long names (--keyword) should be all lowercase
!!
!! o The simplest way to have short names is to suffix the long
!! name with :LETTER If this syntax is used then logical shorts
!! may be combined on the command line and -- and - prefixes are
!! strictly enforced.
!!
!! mapping of short names to long names not using the
!! --LONGNAME:SHORTNAME syntax is demonstrated in the manpage
!! for SPECIFIED(3f).
!!
!! o A very special behavior occurs if the keyword name ends in ::.
!! The next parameter is taken as a value even if it starts with -.
!! This is not generally recommended but is noted here for
!! completeness.
!!
!! o to define a zero-length allocatable array make the
!! value a delimiter (usually a comma).
!!
!! o all unused values go into the character array UNNAMED
!!
!! o If the prototype ends with "--" a special mode is turned
!! on where anything after "--" on input goes into the variable
!! REMAINING and the array ARGS instead of becoming elements in
!! the UNNAMED array. This is not needed for normal processing.
!!
!!##USAGE
!! When invoking the program line note that (subject to change) the
!! following variations from other common command-line parsers:
!!
!! o Long names should be all lowercase and always more than one
!! character.
!!
!! o values for duplicate keywords are appended together with a space
!! separator when a command line is executed.
!!
!! o numeric keywords are not allowed; but this allows
!! negative numbers to be used as values.
!!
!! o Although not generally recommended you can equivalence
!! keywords (usually for multi-lingual support). Be aware that
!! specifying both names of an equivalenced keyword on a command
!! line will have undefined results (currently, their ASCII
!! alphabetical order will define what the Fortran variable
!! values become).
!!
!! The second of the names should only be called with a
!! GET_ARGS*(3f) routine if the SPECIFIED(3f) function is .TRUE.
!! for that name.
!!
!! Note that allocatable arrays cannot be EQUIVALENCEd in Fortran.
!!
!! o short keywords cannot be combined unless they were defined
!! using the --LONGNAME:SHORTNAME syntax. Even then -a -b -c
!! is required not -abc unless all the keywords are logicals
!! (Boolean keys).
!!
!! o shuffling is not supported. Values should follow their
!! keywords.
!!
!! o if a parameter value of just "-" is supplied it is
!! converted to the string "stdin".
!!
!! o values not matching a keyword go into the character
!! array "UNUSED".
!!
!! o if the keyword "--" is encountered the rest of the
!! command arguments go into the character array "UNUSED".
!!##EXAMPLE
!!
!! Sample program:
!!
!! program demo_set_args
!! use M_CLI2, only : filenames=>unnamed, set_args, get_args
!! use M_CLI2, only : get_args_fixed_size
!! implicit none
!! integer :: i
!! ! DEFINE ARGS
!! real :: x, y, z
!! real :: p(3)
!! character(len=:),allocatable :: title
!! logical :: l, lbig
!! integer,allocatable :: ints(:)
!! !
!! ! DEFINE COMMAND (TO SET INITIAL VALUES AND ALLOWED KEYWORDS)
!! ! AND READ COMMAND LINE
!! call set_args(' &
!! ! reals
!! & -x 1 -y 2.3 -z 3.4e2 &
!! ! integer array
!! & -p -1,-2,-3 &
!! ! always double-quote strings
!! & --title "my title" &
!! ! set all logical values to F or T.
!! & -l F -L F &
!! ! set allocatable size to zero if you like by using a delimiter
!! & -ints , &
!! ! string should be a single character at a minimum
!! & --label " " &
!! & ')
!! ! ASSIGN VALUES TO ELEMENTS
!! ! SCALARS
!! call get_args('x',x)
!! call get_args('y',y)
!! call get_args('z',z)
!! call get_args('l',l)
!! call get_args('L',lbig)
!! call get_args('ints',ints) ! ALLOCATABLE ARRAY
!! call get_args('title',title) ! ALLOCATABLE STRING
!! call get_args_fixed_size('p',p) ! NON-ALLOCATABLE ARRAY
!! ! USE VALUES
!! write(*,*)'x=',x
!! write(*,*)'y=',y
!! write(*,*)'z=',z
!! write(*,*)'p=',p
!! write(*,*)'title=',title
!! write(*,*)'ints=',ints
!! write(*,*)'l=',l
!! write(*,*)'L=',lbig
!! ! UNNAMED VALUES
!! if(size(filenames).gt.0)then
!! write(*,'(i6.6,3a)')(i,'[',filenames(i),']',i=1,size(filenames))
!! endif
!! end program demo_set_args
!!
!!##RESPONSE FILES
!!
!! If you have no interest in using external files as abbreviations
!! you can ignore this section. Otherwise, before calling set_args(3f)
!! add:
!!
!! use M_CLI2, only : CLI_response_file
!! CLI_response_file=.true.
!!
!! M_CLI2 Response files are small files containing CLI (Command Line
!! Interface) arguments that end with ".rsp" that can be used when command
!! lines are so long that they would exceed line length limits or so complex
!! that it is useful to have a platform-independent method of creating
!! an abbreviation.
!!
!! Shell aliases and scripts are often used for similar purposes (and
!! allow for much more complex conditional execution, of course), but
!! they generally cannot be used to overcome line length limits and are
!! typically platform-specific.
!!
!! Examples of commands that support similar response files are the Clang
!! and Intel compilers, although there is no standard format for the files.
!!
!! They are read if you add options of the syntax "@NAME" as the FIRST
!! parameters on your program command line calls. They are not recursive --
!! that is, an option in a response file cannot be given the value "@NAME2"
!! to call another response file.
!!
!! Note that more than one response name may appear on a command line.
!!
!! They are case-sensitive names.
!!
!! LOCATING RESPONSE FILES
!!
!! A search for the response file always starts with the current directory.
!! The search then proceeds to look in any additional directories specified
!! with the colon-delimited environment variable CLI_RESPONSE_PATH.
!!
!! The first resource file found that results in lines being processed
!! will be used and processing stops after that first match is found. If
!! no match is found an error occurs and the program is stopped.
!!
!! RESPONSE FILE SECTIONS
!!
!! A simple response file just has options for calling the program in it
!! prefixed with the word "options".
!! But they can also contain section headers to denote selections that are
!! only executed when a specific OS is being used, print messages, and
!! execute system commands.
!!
!! SEARCHING FOR OSTYPE IN REGULAR FILES
!!
!! So assuming the name @NAME was specified on the command line a file
!! named NAME.rsp will be searched for in all the search directories
!! and then in that file a string that starts with the string @OSTYPE
!! (if the environment variables $OS and $OSTYPE are not blank. $OSTYPE
!! takes precedence over $OS).
!!
!! SEARCHING FOR UNLABELED DIRECTIVES IN REGULAR FILES
!!
!! Then, the same files will be searched for lines above any line starting
!! with "@". That is, if there is no special section for the current OS
!! it just looks at the top of the file for unlabeled options.
!!
!! SEARCHING FOR OSTYPE AND NAME IN THE COMPOUND FILE
!!
!! In addition or instead of files with the same name as the @NAME option
!! on the command line, you can have one file named after the executable
!! name that contains multiple abbreviation names.
!!
!! So if your program executable is named EXEC you create a single file
!! called EXEC.rsp and can append all the simple files described above
!! separating them with lines of the form @OSTYPE@NAME or just @NAME.
!!
!! So if no specific file for the abbreviation is found a file called
!! "EXEC.rsp" is searched for where "EXEC" is the name of the executable.
!! This file is always a "compound" response file that uses the following format:
!!
!! Any compound EXEC.rsp file found in the current or searched directories
!! will be searched for the string @OSTYPE@NAME first.
!!
!! Then if nothing is found, the less specific line @NAME is searched for.
!!
!! THE SEARCH IS OVER
!!
!! Sounds complicated but actually works quite intuitively. Make a file in
!! the current directory and put options in it and it will be used. If that
!! file ends up needing different cases for different platforms add a line
!! like "@Linux" to the file and some more lines and that will only be
!! executed if the environment variable OSTYPE or OS is "Linux". If no match
!! is found for named sections the lines at the top before any "@" lines
!! will be used as a default if no match is found.
!!
!! If you end up using a lot of files like this you can combine them all
!! together and put them into a file called "program_name".rsp and just
!! put lines like @NAME or @OSTYPE@NAME at that top of each selection.
!!
!! Now, back to the details on just what you can put in the files.
!!
!!##SPECIFICATION FOR RESPONSE FILES
!!
!! SIMPLE RESPONSE FILES
!!
!! The first word of a line is special and has the following meanings:
!!
!! options|- Command options following the rules of the SET_ARGS(3f)
!! prototype. So
!! o It is preferred to specify a value for all options.
!! o double-quote strings.
!! o give a blank string value as " ".
!! o use F|T for lists of logicals,
!! o lists of numbers should be comma-delimited.
!! o --usage, --help, --version, --verbose, and unknown
!! options are ignored.
!!
!! comment|# Line is a comment line
!! system|! System command.
!! System commands are executed as a simple call to
!! system (so a cd(1) or setting a shell variable
!! would not effect subsequent lines, for example)
!! print|> Message to screen
!! stop display message and stop program.
!!
!!
!!
!! So if a program that does nothing but echos its parameters
!!
!! program testit
!! use M_CLI2, only : set_args, rget, sget, lget
!! use M_CLI2, only : CLI_response_file
!! implicit none
!! real :: x,y ; namelist/args/ x,y
!! character(len=:),allocatable :: title ; namelist/args/ title
!! logical :: big ; namelist/args/ big
!! CLI_response_file=.true.
!! call set_args('-x 10.0 -y 20.0 --title "my title" --big F')
!! x=rget('x')
!! y=rget('y')
!! title=sget('title')
!! big=lget('big')
!! write(*,nml=args)
!! end program testit
!!
!! And a file in the current directory called "a.rsp" contains
!!
!! # defaults for project A
!! options -x 1000 -y 9999
!! options --title " "
!! options --big T
!!
!! The program could be called with
!!
!! $myprog # normal call
!! X=10.0 Y=20.0 TITLE="my title"
!!
!! $myprog @a # change defaults as specified in "a.rsp"
!! X=1000.0 Y=9999.0 TITLE=" "
!!
!! # change defaults but use any option as normal to override defaults
!! $myprog @a -y 1234
!! X=1000.0 Y=1234.0 TITLE=" "
!!
!! COMPOUND RESPONSE FILES
!!
!! A compound response file has the same basename as the executable with a
!! ".rsp" suffix added. So if your program is named "myprg" the filename
!! must be "myprg.rsp".
!!
!! Note that here `basename` means the last leaf of the
!! name of the program as returned by the Fortran intrinsic
!! GET_COMMAND_ARGUMENT(0,...) trimmed of anything after a period ("."),
!! so it is a good idea not to use hidden files.
!!
!! Unlike simple response files compound response files can contain multiple
!! setting names.
!!
!! Specifically in a compound file
!! if the environment variable $OSTYPE (first) or $OS is set the first search
!! will be for a line of the form (no leading spaces should be used):
!!
!! @OSTYPE@alias_name
!!
!! If no match or if the environment variables $OSTYPE and $OS were not
!! set or a match is not found then a line of the form
!!
!! @alias_name
!!
!! is searched for in simple or compound files. If found subsequent lines
!! will be ignored that start with "@" until a line not starting with
!! "@" is encountered. Lines will then be processed until another line
!! starting with "@" is found or end-of-file is encountered.
!!
!! COMPOUND RESPONSE FILE EXAMPLE
!! An example compound file
!!
!! #################
!! @if
!! > RUNNING TESTS USING RELEASE VERSION AND ifort
!! options test --release --compiler ifort
!! #################
!! @gf
!! > RUNNING TESTS USING RELEASE VERSION AND gfortran
!! options test --release --compiler gfortran
!! #################
!! @nv
!! > RUNNING TESTS USING RELEASE VERSION AND nvfortran
!! options test --release --compiler nvfortran
!! #################
!! @nag
!! > RUNNING TESTS USING RELEASE VERSION AND nagfor
!! options test --release --compiler nagfor
!! #
!! #################
!! # OS-specific example:
!! @Linux@install
!! #
!! # install executables in directory (assuming install(1) exists)
!! #
!! system mkdir -p ~/.local/bin
!! options run --release T --runner "install -vbp -m 0711 -t ~/.local/bin"
!! @install
!! STOP INSTALL NOT SUPPORTED ON THIS PLATFORM OR $OSTYPE NOT SET
!! #
!! #################
!! @fpm@testall
!! #
!! !fpm test --compiler nvfortran
!! !fpm test --compiler ifort
!! !fpm test --compiler gfortran
!! !fpm test --compiler nagfor
!! STOP tests complete. Any additional parameters were ignored
!! #################
!!
!! Would be used like
!!
!! fpm @install
!! fpm @nag --
!! fpm @testall
!!
!! NOTES
!!
!! The intel Fortran compiler now calls the response files "indirect
!! files" and does not add the implied suffix ".rsp" to the files
!! anymore. It also allows the @NAME syntax anywhere on the command line,
!! not just at the beginning. -- 20201212
!!
!!##AUTHOR
!! John S. Urban, 2019
!!
!!##LICENSE
!! Public Domain
!===================================================================================================================================
!()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()!
!===================================================================================================================================
subroutine set_args(prototype,help_text,version_text,string,prefix,ierr,errmsg)
! ident_2="@(#) M_CLI2 set_args(3f) parse prototype string"
character(len=*),intent(in) :: prototype
character(len=*),intent(in),optional :: help_text(:)
character(len=*),intent(in),optional :: version_text(:)
character(len=*),intent(in),optional :: string
character(len=*),intent(in),optional :: prefix
integer,intent(out),optional :: ierr
character(len=:),intent(out),allocatable,optional :: errmsg
character(len=:),allocatable :: hold ! stores command line argument
integer :: ibig
G_response=CLI_RESPONSE_FILE
G_options_only=.false.
G_append=.true.
G_passed_in=''
G_STOP=0
G_STOP_MESSAGE=''
if(present(prefix))then
G_PREFIX=prefix
else
G_PREFIX=''
endif
if(present(ierr))then
G_QUIET=.true.
else
G_QUIET=.false.
endif
ibig=longest_command_argument() ! bug in gfortran. len=0 should be fine
IF(ALLOCATED(UNNAMED)) DEALLOCATE(UNNAMED)
ALLOCATE(CHARACTER(LEN=IBIG) :: UNNAMED(0))
if(allocated(args)) deallocate(args)
allocate(character(len=ibig) :: args(0))
call wipe_dictionary()
hold='--version F --usage F --help F --version F '//adjustl(prototype)
call prototype_and_cmd_args_to_nlist(hold,string)
if(allocated(G_RESPONSE_IGNORED))then
if(debug_m_cli2)write(*,gen)'<DEBUG>SET_ARGS:G_RESPONSE_IGNORED:',G_RESPONSE_IGNORED
if(size(unnamed).ne.0)write(*,*)'LOGIC ERROR'
call split(G_RESPONSE_IGNORED,unnamed)
endif
if(.not.allocated(unnamed))then
allocate(character(len=0) :: unnamed(0))
endif
if(.not.allocated(args))then
allocate(character(len=0) :: args(0))
endif
call check_commandline(help_text,version_text) ! process --help, --version, --usage
if(present(ierr))then
ierr=G_STOP
endif
if(present(errmsg))then
errmsg=G_STOP_MESSAGE
endif
end subroutine set_args
!===================================================================================================================================
!()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()!
!===================================================================================================================================
!>
!!##NAME
!! get_subcommand(3f) - [ARGUMENTS:M_CLI2] special-case routine for
!! handling subcommands on a command line
!! (LICENSE:PD)
!!
!!##SYNOPSIS
!!
!! function get_subcommand()
!!
!! character(len=:),allocatable :: get_subcommand
!!
!!##DESCRIPTION
!! In the special case when creating a program with subcommands it
!! is assumed the first word on the command line is the subcommand. A
!! routine is required to handle response file processing, therefore
!! this routine (optionally processing response files) returns that
!! first word as the subcommand name.
!!
!! It should not be used by programs not building a more elaborate
!! command with subcommands.
!!
!!##RETURNS
!! NAME name of subcommand
!!
!!##EXAMPLE
!!
!! Sample program:
!!
!! program demo_get_subcommand
!! !x! SUBCOMMANDS
!! !x! For a command with subcommands like git(1)
!! !x! you can make separate namelists for each subcommand.
!! !x! You can call this program which has two subcommands (run, test),
!! !x! like this:
!! !x! demo_get_subcommand --help
!! !x! demo_get_subcommand run -x -y -z -title -l -L
!! !x! demo_get_subcommand test -title -l -L -testname
!! !x! demo_get_subcommand run --help
!! implicit none
!! !x! DEFINE VALUES TO USE AS ARGUMENTS WITH INITIAL VALUES
!! real :: x=-999.0,y=-999.0,z=-999.0
!! character(len=80) :: title="not set"
!! logical :: l=.false.
!! logical :: l_=.false.
!! character(len=80) :: testname="not set"
!! character(len=20) :: name
!! call parse(name) !x! DEFINE AND PARSE COMMAND LINE
!! !x! ALL DONE CRACKING THE COMMAND LINE.
!! !x! USE THE VALUES IN YOUR PROGRAM.
!! write(*,*)'command was ',name
!! write(*,*)'x,y,z .... ',x,y,z
!! write(*,*)'title .... ',title
!! write(*,*)'l,l_ ..... ',l,l_
!! write(*,*)'testname . ',testname
!! contains
!! subroutine parse(name)
!! !x! PUT EVERYTHING TO DO WITH COMMAND PARSING HERE FOR CLARITY
!! use M_CLI2, only : set_args, get_args, get_args_fixed_length
!! use M_CLI2, only : get_subcommand
!! use M_CLI2, only : CLI_RESPONSE_FILE
!! character(len=*) :: name ! the subcommand name
!! character(len=:),allocatable :: help_text(:), version_text(:)