-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatchlib.bat
1762 lines (1568 loc) · 49.4 KB
/
batchlib.bat
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
:entry_point
@goto main
rem ############################################################################
rem License
rem ############################################################################
:license
echo MIT No Attribution
echo=
echo Copyright 2017-2025 wthe22
echo=
echo Permission is hereby granted, free of charge, to any person obtaining a copy of this
echo software and associated documentation files (the "Software"), to deal in the Software
echo without restriction, including without limitation the rights to use, copy, modify,
echo merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
echo permit persons to whom the Software is furnished to do so.
echo=
echo THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
echo INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
echo PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
echo HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
echo OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
echo SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
exit /b 0
rem ############################################################################
rem Documentation
rem ############################################################################
:doc.man
:: NAME
:: batchlib - batch script library
::
:: SYNOPSIS
:: batchlib
:: batchlib (-h|--help)
:: batchlib -c :<label> [arguments] ...
:: batchlib build <input_file> [backup_name]
:: batchlib debug <library> :<label> [arguments] ...
:: batchlib test [library]
:: batchlib template <name>
::
:: CALL SUBCOMMAND
:: batchlib -c :<label> [arguments] ...
::
:: Make batchlib CALL the specified label with the following arguments.
::
:: BUILD SUBCOMMAND
:: batchlib build <input_file> [backup_name]
::
:: Add/update dependencies of a file (made from the new script template).
:: Dependencies are found in 'install_requires' in lib.dependencies() and
:: 'extra_requires' is ignored. Build is aborted if any errors occur.
:: Please keep codes between 'entry_point' and 'EOF'. Codes beyond that will
:: be removed.
::
:: POSITIONAL ARGUMENTS
:: input_file
:: Path of the input file to build.
::
:: backup_name
:: Path of the backup file.
::
:: RUN SUBCOMMAND
:: batchlib run <library> :<label> [arguments] ...
::
:: Run the library and call :LABEL with the ARGUMENTS.
::
:: This subcommand is not available in the minified version.
::
:: DEBUG SUBCOMMAND
:: batchlib debug <library> :<label> [arguments] ...
::
:: Run the library and call :LABEL with the ARGUMENTS.
:: Additional debugging features are:
:: - The quicktest() and unittest() libraries are included.
:: - Debug commands are executed and output is redirected to STDERR
:: if 'debug' variable is not defined.
:: - Library is always rebuilt before running.
::
:: This subcommand is not available in the minified version.
::
:: MAN SUBCOMMAND
:: batchlib man [library]
::
:: Shows documentation of a library. If no library name is specified,
:: it will show documentation of batchlib.
::
:: This subcommand is not available in the minified version.
::
:: TEST SUBCOMMAND
:: batchlib test [library]
::
:: Run unittests of a library. If no library is given, it will
:: test all libraries instead.
::
:: This subcommand is not available in the minified version.
::
:: TEMPLATE SUBCOMMAND
:: batchlib template <name>
::
:: Create the specified template and output to STDOUT.
:: Available templates: minified.
::
:: This subcommand have no templates in the minified version.
exit /b 0
:doc.argument_syntax
:: ARGUMENT SYNTAX
:: Description about how arguments are processed by functions. Here is a list
:: of possible arguments syntax used in the library and its usage example:
::
:: function_name <number>
:: call :function_name 1
::
:: Angle brackets <> means it is required. Number is a positional argument
:: and it is required. Errors might occur if argument is not specified.
::
:: function_name <number ...>
:: call :function_name "1 1 1"
::
:: Number can be specified multiple times within a pair of quotes.
:: Values are accessed through %~1.
::
:: function_name <number> ...
:: call :function_name 1 "2" "3" 4
::
:: Number can be specified multiple times and they must not share the
:: same pair of quotes. Values are accessed through %*.
::
:: function_name [number]
:: call :function_name # OK, because number is optional
:: call :function_name 1
::
:: Square brackets [] means it is optional.
::
:: function_name [-n|--number]
:: call :function_name -n
:: call :function_name --number
::
:: -n and --number are options. They can be used interchangeably
:: (they are just the same thing).
::
:: function_name [-n number]
:: call :function_name -n 1
:: call :function_name -n # Error: missing expected argument
::
:: Number must be specified after the -n flag. Errors might occur if
:: the expected argument is not specified.
::
:: function_name [--number] <message>
:: call :function_name --number "Hello" # OK
:: call :function_name "Hello" --number # Also OK
::
:: An option can be placed anywhere if it occurs before the positional
:: arguments.
::
:: function_name <message> [--number]
:: call :function_name "Hello" --number # OK
:: call :function_name --number "Hello" # Error: wrong position
::
:: An option must occur in the same position if it occurs after the
:: positional arguments. In this case, --number must be specified in
:: the second position if used.
::
:: ARGUMENT VALIDATION
:: Some libraries have argument validation. Usually they can be identified by
:: having the 'invalid argument' exit status. However, most library did not
:: have argument validation. It assumes that the given argument syntax and
:: values are valid. Make sure you validate the value first before passing it
:: to the function (e.g. if the function only accepts an integer, make sure
:: you pass an integer too, not alphabets, symbols, hexadecimal, etc.).
exit /b 0
:doc._guides
:: Variable names: [A-Za-Z_][A-Za-z0-9_.][A-Za-z0-9]
:: - Starts with an alphabet or underscore
:: - Only use alphabets, numbers, underscores, dashes, and dots
:: - Ends with an alphabet, underscore, or number
:: - Use variable names as a namespace
::
:: - external: something that is stored in other files
exit /b 0
:doc.help
echo usage:
echo batchlib
echo Run batchlib in interactive mode
echo=
echo batchlib (-h^|--help)
echo Show this help
echo=
echo batchlib -c :^<label^> [arguments] ...
echo Call the specified label with arguments
echo=
echo batchlib build ^<input_file^> [backup_name]
echo Add/update dependency of a file
echo=
echo batchlib run ^<library^> :^<label^> [arguments] ...
echo Run a function in the library (Not available in minified version)
echo=
echo batchlib debug ^<library^> :^<label^> [arguments] ...
echo Debug a library (Not available in minified version)
echo=
echo batchlib man [library]
echo Shows documentation of a library or the batchlib itself
echo (Not available in minified version)
echo=
echo batchlib test [library]
echo Run unittest to a library (Not available in minified version)
echo=
echo batchlib template ^<name^>
echo Create the specified template and output to STDOUT.
exit /b 0
rem ############################################################################
rem Changelog
rem ############################################################################
:changelog
:: TLDR
:: Simplifying usage with some new features, and license change
::
:: - New subcommand: run
:: - New functions: list_lf2set, ini_edit, get_net_iface
:: - Bug fixes: color_print, endlocal, quicktest, strip, input_yesno
:: - Behavior changes: quicktest, check_path, strip
:: - Global variable usage changes: functions_list, functions_range, macroify,
:: strip
:: - Breaking changes: argparse, coderender, unittest, endlocal
:: - Deprecated / removed: ut_fmt_basic, argparse
:: - License: Remove attribution requirement (MIT No Attribution)
::
:: Core
:: - Include unittest() to library in debug mode
:: - Remove '-c' subcommand requirement to build script
:: - Add build script warning when ':entry_point' is not at line 1
::
:: Library
:: - Functions now initialize required constants and macros, then saving them as
:: shared global variables
:: - Recategorize functions
:: - list_lf2set(): New! Similar to list2set() but with Line Feed seperator
:: - ini_edit(): New! INI configuration file editor
:: - get_net_iface(): NEW! Get network interface data (experimental)
::
:: - argparse():
:: - Reworked spec syntax
:: - Help flag detection and auto-generated usage syntax
:: - Required/optional spec detection
:: - A single flag can now capture multiple arguments
:: - check_path(): Adjust exit status so invalid argument
:: errors are distinguishable
:: - coderender():
:: - Add option to generate codes to render
:: - Changed the way arguments are passed to template
:: - combi_wcdir(): Simplify code by using list_lf2set()
:: - endlocal():
:: - Fix string gets executed when it contains ampersand
:: - Add ability to quit multiple stacks of setlocal
:: - Add ability to exit to access parent context
:: - Function signature change
:: - Add support for returning LF characters in EnableDelayedExpansion
:: - get_os(): Function can now detect Windows 11
:: - input_yesno(): Fix value not returned if the value is null
:: - nroot(): Improve readabilty
:: - prime(): Add nroot dependency and improve readabilty
:: - quicktest():
:: - Empty label string now triggers auto detect label
:: - Use outcome message as-is to prevent errors from unquoted string
:: - Error in tests.setup() will abort the test,
:: making behavior consistent with unittest
:: - strip():
:: - Special characters in variable can now be safely processed
:: - Double quotes can now be stripped
:: - Support stripping multiple characters at the same time
:: - Function can now be converted to macro by using macroify()
:: - timeit(): Add unittest, reformat macro code
:: - timeleft(): Add macro
:: - ut_fmt_basic(): Removed / merged into unittest() as 'basic' output.
:: - unittest():
:: - Remove '-s' flag
:: - Rework and simplify usage of '-o' flag
:: - Added an experimental TAP output
:: - updater(): Remove '-c' subcommand requirement
:: - version_parse(): Add python testing code
::
:: Documentation
:: - Fix argument syntax of: quicktest, hexlify, argparse
:: - Fix demo of: unittest
:: - Improve some documentations
:: - macroify(): Add details on creating and using macros
:: - Template subcommand will list available template when no name is given
:: - Mark functions as required or optional in template.bat
:: - Add new subcommand to view documentation of a script
exit /b 0
:changelog.dev
:: - ini_parse():
:: - Rename from ini_parse()
:: - Leave value as-is
:: - Add command to get sections and keys
:: - Use variables and macro to improve performance
:: - coderender():
:: - Add option to generate codes to render
:: - Changed the way arguments are passed to template
:: - endlocal():
:: - Add ability to exit to access parent context
:: - Add support for returning LF characters in EnableDelayedExpansion
:: - strip():
:: - Support stripping multiple characters at the same time
:: - input_string(), input_path(): Use new syntax of endlocal()
:: - macroify()
:: - Add a dot in front of global variables
:: - color_print(): Reduce chances of overwriting other temp files
:: - diffbin(), hexlify(), wait(), watchvar():
:: - Add prefix to temporary files
:: - functions_list(), functions_range(), ini_parse(), macroify(), strip():
:: - Use shared global variables for constants and macros
exit /b 0
rem ############################################################################
rem Metadata
rem ############################################################################
:metadata [return_prefix]
set "%~1name=batchlib"
set "%~1version=3.2b"
set "%~1authors=wthe22"
set "%~1license=MIT No Attribution"
set "%~1description=Batch Script Library"
set "%~1release_date=01/29/2025" :: mm/dd/YYYY
set "%~1url=https://github.com/wthe22/batchlib"
set "%~1download_url=https://raw.githubusercontent.com/wthe22/batchlib/master/batchlib.bat"
exit /b 0
:about
setlocal EnableDelayedExpansion
call :metadata
echo !description! / !name! !version! ^(!release_date!^)
echo=
echo A collection of functions/snippets for batch script
echo Created to make batch scripting easier
echo=
echo Updated on !release_date!
echo=
echo Feel free to use, share, or modify this script for your projects :)
echo Visit http://winscr.blogspot.com/ or https://github.com/wthe22/batch-scripts
echo for more scripts^^!
echo=
echo=
echo Copyright (C) 2017-2025 by !authors!
echo Licensed under !license!
endlocal
exit /b 0
rem ############################################################################
rem Configurations
rem ############################################################################
:config
call :config.default
call :config.preference
exit /b 0
:config.default
rem Default/common configurations. DO NOT MODIFY
rem Directory for temporary files
set "tmp_dir=!tmp!\!SOFTWARE.name!"
rem Directory to keep library, builds, etc.
set "lib_dir=%~dp0lib"
set "build_dir=%~dp0build"
exit /b 0
:config.preference
rem Configurations to change/override
:: set "tmp_dir=%~dp0tmp\tmp"
exit /b 0
rem ############################################################################
rem Main
rem ############################################################################
:main
@if ^"%1^" == "-c" @goto subcommand.call
@setlocal EnableDelayedExpansion EnableExtensions
@echo off
if ^"%1^" == "-h" goto doc.help
if ^"%1^" == "--help" goto doc.help
call :metadata SOFTWARE.
call :config
call :common_setup
if ^"%1^" == "" (
call :main_script
) else call :subcommand.%*
set "exit_code=!errorlevel!"
call :common_cleanup
@exit /b !exit_code!
:common_setup
set "flags.is_minified="
call :flags.is_minified 2> nul && set "flags.is_minified=true"
set "lib="
call :true 2> nul || set "lib=:lib.call "
set "no_cleanup="
if defined flags.is_minified (
set "lib_dir="
set "build_dir="
)
for %%p in (
tmp_dir
lib_dir
build_dir
) do if defined %%p if not exist "!%%p!" md "!%%p!"
call :Library.unload_info
call :Library.read_names
call :Library.read_dependencies
if not defined flags.is_minified (
call :LibBuild.remove_orphans
)
exit /b 0
:common_cleanup
if defined no_cleanup exit /b 0
for %%p in (
tmp_dir
) do if defined %%p if exist "!%%p!" rd /s /q "!%%p!"
exit /b 0
:main_script
call :true 2> nul || call :self_build
call :Library.read_args
call :Category.unload_info
call :Category.load
call :main_menu
cls
exit /b
:subcommand.build <file>
call :build_script %*
exit /b
:subcommand.run <library> :<label> [arguments] ...
set "library=%~1"
call :LibBuild.update "!library!" || exit /b 3
endlocal & (
cd /d "%build_dir%"
set "tmp_dir=%tmp_dir%"
)
call %*
exit /b
:subcommand.debug <library> :<label> [arguments] ...
set "library=%~1"
for %%v in ("Library_!library!.extra_requires") do set "%%~v=!%%~v! quicktest unittest"
call :LibBuild.build "!library!" || exit /b 3
endlocal & (
cd /d "%build_dir%"
set "tmp_dir=%tmp_dir%"
)
if not defined debug set "debug=/? > nul & 1>&2"
call %*
set "no_cleanup=true"
exit /b
:subcommand.man <library>
if ^"%1^" == "" (
call :functions_range _range "%~f0" doc.man
call :readline "%~f0" !_range! 1:-1 4
) else (
set "_library=%~1"
set "_library_source=!lib_dir!\!_library!.bat"
call :functions_range _range "!_library_source!" "doc.man" && (
call :readline "!_library_source!" !_range! 1:-1 4
)
)
exit /b 0
:subcommand.test <library>
call :tests.run_lib_test %1
exit /b
:subcommand.template <name>
set "template_name=%~1"
if not defined template_name (
call :functions_match labels "%~f0" "template.*"
set "_templates= "
for %%l in (!labels!) do (
for /f "tokens=2 delims=." %%a in ("%%l") do (
set "_templates=!_templates: %%a = !%%a "
)
)
set "_templates=!_templates:~1,-1!"
echo Available templates: !_templates!
exit /b 0
)
call :coderender "%~f0" "template.!template_name!"
exit /b
:subcommand.call -c :<label> [arguments] ...
@(
setlocal DisableDelayedExpansion
call set command=%%*
setlocal EnableDelayedExpansion
for /f "tokens=1* delims== " %%a in ("!command!") do @(
endlocal
endlocal
call %%b
)
)
@exit /b
rem ############################################################################
rem User Interfaces
rem ############################################################################
:ui
exit /b 0
:main_menu
set "user_input="
cls
echo !SOFTWARE.DESCRIPTION! !SOFTWARE.VERSION!
echo=
echo 1. Browse documentation
echo 2. Use command line
echo 3. Test Libraries
echo 4. Generate minified version
echo 5. Build/add dependencies to a script
echo 6. Reload Library
echo=
echo H. Help
echo 0. Exit
echo=
echo What do you want to do?
set /p "user_input="
echo=
if "!user_input!" == "0" exit /b 0
if "!user_input!" == "1" call :browse_lib
if "!user_input!" == "2" call :conemu
if "!user_input!" == "3" (
call :tests.run_lib_test
pause
goto main_menu
)
if "!user_input!" == "4" call :new_template_menu "minified" "Minified Script"
if "!user_input!" == "5" call :build_menu
if "!user_input!" == "6" (
call :Library.unload_info
call :Library.read_names
call :Library.read_dependencies
call :Library.read_args
call :Category.unload_info
call :Category.load
call :LibBuild.remove_orphans
pause
goto main_menu
)
if /i "!user_input!" == "H" call :help_menu
goto main_menu
:self_build
setlocal EnableDelayedExpansion
echo Adding/updating dependencies to this script...
set "lib=:lib.call "
call :build_script "%~f0"
echo Build done [exit code !errorlevel!]
pause
exit /b 0
:help_menu
set "user_input="
cls
echo 1. Usage
echo 2. Library Argument Syntax
echo 3. Change Log
echo=
echo U. Check for Update
echo A. About Script
echo 0. Exit
echo=
echo What do you want to do?
set /p "user_input="
echo=
if "!user_input!" == "0" exit /b 0
if "!user_input!" == "1" call :show_docs doc.man
if "!user_input!" == "2" call :show_docs doc.argument_syntax
if "!user_input!" == "3" call :show_docs changelog
if /i "!user_input!" == "A" (
cls
call :about
echo=
pause
goto help_menu
)
if /i "!user_input!" == "U" (
call :updater -n "%~f0" && (
echo Get the latest version at !SOFTWARE.url!
)
pause
goto help_menu
)
goto help_menu
:browse_lib
set "selected.quit="
set "selected.category="
set "selected.function="
:browse_lib._loop
if defined selected.quit exit /b 2
if not defined selected.category (
call :InputCategory selected.category || (
set "selected.quit=true"
goto browse_lib._loop
)
)
if not defined selected.function (
call :InputFunction selected.function !selected.category! || (
set "selected.category="
goto browse_lib._loop
)
)
call :LibMenu !selected.function!
set "selected.function="
goto browse_lib._loop
:InputCategory <return_var>
set "_selected="
call :InputCategory.select_menu
set "%~1=!_selected!"
if not defined _selected exit /b 2
exit /b 0
#+++
:InputCategory.select_menu
set "user_input="
cls
call :InputCategory.get_item list
echo=
echo S. Search function
echo 0. Back
echo=
echo Which category do you want to choose?
set /p "user_input="
echo=
if "!user_input!" == "0" exit /b 2
if /i "!user_input!" == "S" (
call :InputCategory.search_menu && exit /b 0
) else call :InputCategory.get_item "!user_input!" && exit /b 0
goto InputCategory.select_menu
#+++
:InputCategory.get_item <list|number>
set "_count=0"
for %%c in (!Category.all!) do (
if not "!Category_%%c.item_count!" == "0" (
set /a "_count+=1"
if /i ^"%1^" == "list" (
set "_count= !_count!"
echo !_count:~-3,3!. !Category_%%c.name! ^(%%c^) [!Category_%%c.item_count!]
) else if "%~1" == "!_count!" set "_selected=%%c"
)
)
if not defined _selected exit /b 2
exit /b 0
#+++
:InputCategory.search_menu
set "user_input="
cls
echo 0. Back
echo=
echo Input search keyword:
set /p "user_input="
echo=
if "!user_input!" == "0" exit /b 2
call :Library.search Category_search.functions "!user_input!"
set "_selected=search"
set "Category_search.name=Search Result for '!user_input!'"
set "Category_search.item_count=0"
for %%f in (!Category_search.functions!) do set /a "Category_search.item_count+=1"
exit /b 0
:InputFunction <return_var> <category>
set "_category=%~2"
set "_selected="
call :InputFunction.select_menu
set "%~1=!_selected!"
if not defined _selected exit /b 2
exit /b 0
#+++
:InputFunction.select_menu
set "user_input="
cls
echo=!Category_%_category%.name!
echo=
set "_count=0"
for %%c in (!_category!) do ( rem
) & for %%f in (!Category_%%c.functions!) do (
set /a "_count+=1"
set "_count= !_count!"
echo !_count:~-3,3!. !Library_%%f.args!
)
echo=
echo 0. Back
echo=
echo Which function do you want to choose?
set /p "user_input="
echo=
if "!user_input!" == "0" exit /b 2
set "_count=0"
for %%c in (!_category!) do ( rem
) & for %%f in (!Category_%%c.functions!) do (
set /a "_count+=1"
if "!user_input!" == "!_count!" (
set "_selected=%%f"
exit /b 0
)
)
goto InputFunction.select_menu
#+++
:InputFunction.get_item <list|number>
set "_count=0"
for %%c in (!_category!) do ( rem
) & for %%f in (!Category_%%c.functions!) do (
set /a "_count+=1"
if /i ^"%1^" == "list" (
set "_count= !_count!"
echo !_count:~-3,3!. !Library_%%f.args!
) else if "%~1" == "!_count!" set "_selected=%%f"
)
if not defined _selected exit /b 2
exit /b 0
:LibMenu <function>
setlocal EnableDelayedExpansion
set "_library=%~1"
set "_library_source=!lib_dir!\!_library!.bat"
set "_library_build=!build_dir!\!_library!.bat"
call "!_library_source!" :lib.dependencies "Library_!_library!." 2> nul
call :LibBuild.update "!_library!"
call :LibMenu.menu
exit /b 0
#+++
:LibMenu.menu
set "user_input="
cls
echo !Library_%_library%.args!
echo=
echo 1. Read manual
echo 2. Run demo
echo 3. Run tests
echo 4. More information
echo 5. View source
echo=
echo 0. Back
echo=
echo What do you want to do?
set /p "user_input="
echo=
if "!user_input!" == "0" exit /b 0
if "!user_input!" == "1" call :LibMenu.read_manual
if "!user_input!" == "2" call :LibMenu.run_demo
if "!user_input!" == "3" call :LibMenu.run_tests
if "!user_input!" == "4" call :LibMenu.show_info
if "!user_input!" == "5" call :LibMenu.view_source
goto LibMenu.menu
:LibMenu.read_manual
cls
call :functions_range _range "!_library_source!" "doc.man" && (
call :readline "!_library_source!" !_range! 1:-1 4
)
echo=
pause
exit /b 0
:LibMenu.run_demo
setlocal EnableDelayedExpansion
cls
call :functions_range _range "!_library_source!" "doc.man" && (
call :readline "!_library_source!" !_range! 1:-1 4
)
echo=
echo ================================================================================
echo=
call "!_library_build!" :doc.demo
echo=
pause
exit /b 0
:LibMenu.run_tests
setlocal EnableDelayedExpansion
call :tests.run_lib_test !_library!
pause
exit /b 0
:LibMenu.show_info
cls
set "category= "
for %%c in (!Library_%_library%.category!) do set "category=!category!!Category_%%c.name!, "
set "category=!category:~1,-2!"
call :Library.rdepends rdepends "!Library.all!" "!_library!" "install"
set "rdepends=!rdepends: %_library% = !"
echo Name : !_library!
echo Argument Syntax : !Library_%_library%.args!
echo Category : !category!
echo=
echo Install requires : !Library_%_library%.install_requires!
echo Extra requires : !Library_%_library%.extra_requires!
echo Required by : !rdepends!
echo=
pause
exit /b 0
:LibMenu.view_source
cls
call :functions_range _range "!_library_source!" "!_library!" && (
call :readline "!_library_source!" !_range!
)
echo=
pause
exit /b 0
:new_template_menu <template> <description>
setlocal EnableDelayedExpansion
set "_template=%~1"
set "_description=%~2"
call :input_path save_file --file --warn-overwrite --optional ^
^ --message "Input new !_description! path (Enter nothing to cancel): " ^
^ || exit /b 2
echo=
echo !_description! path:
echo !save_file!
echo=
echo Generating...
set "start_time=!time!"
call :coderender "%~f0" "template.!_template!" > "!save_file!"
call :difftime time_taken "!time!" "!start_time!"
call :ftime time_taken !time_taken!
echo=
echo Done in !time_taken!
pause
exit /b 0
:build_menu
call :input_path script_file --file --exist --optional ^
^ --message "Input script path (Enter nothing to cancel): " ^
^ || exit /b 2
call :input_path backup_file --file --warn-overwrite --optional ^
^ --message "Input backup path (optional): "
echo=
echo Generating...
set "start_time=!time!"
if defined backup_file (
call :build_script "!script_file!" "!backup_file!"
) else call :build_script "!script_file!"
call :difftime time_taken "!time!" "!start_time!"
call :ftime time_taken !time_taken!
echo=
echo Done in !time_taken!
pause
exit /b 0
:show_docs <label>
cls
call :functions_range _range "%~f0" %1
call :readline "%~f0" !_range! 1:-1 4
echo=
pause
exit /b 0
rem ############################################################################
rem Core
rem ############################################################################
:core
exit /b 0
:build_script <input_file> [backup_name]
setlocal EnableDelayedExpansion
set "_input_file=%~f1"
set "_backup_file=%~f2"
cd /d "!tmp_dir!" 2> nul || cd /d "!tmp!"
if defined flags.is_minified set "lib="
call %lib%:functions_range _range "!_input_file!" "lib.dependencies" || exit /b 3
call %lib%:readline "!_input_file!" !_range! > "_lib_dependencies.bat" || exit /b 3
call "_lib_dependencies.bat" || exit /b 3
call :Library.depends _dep "!install_requires!" || exit /b 3
call :Library.unload_info
call %lib%:functions_range _range "!_input_file!" "entry_point EOF" || exit /b 3
for /f "tokens=1,4 delims=: " %%a in ("!_range!") do (
set "_range=%%a:%%b"
if %%a GTR 1 (
1>&2 echo%0: warning: Expected ':entry_point' label at line 1, found at line %%a
)
)
> "_build_script.tmp" (
call %lib%:readline "!_input_file!" !_range! || exit /b 3
echo=
echo=
echo :: Automatically Added by !SOFTWARE.name! !SOFTWARE.VERSION! on !date! !time!
echo=
if defined flags.is_minified (
call :functions_range _ranges "%~f0" "!_dep!" || exit /b 3
for %%r in (!_ranges!) do (
call :readline "%~f0" %%r || exit /b 3
echo=
echo=
)
) else (
for %%l in (!_dep!) do (
call %lib%:functions_range _range "!lib_dir!\%%l.bat" "%%l" || exit /b 3
call %lib%:readline "!lib_dir!\%%l.bat" !_range! || exit /b 3
echo=
echo=
)
)
) || exit /b 3
if defined _backup_file (
copy /b /v /y "!_input_file!" "!_backup_file!" > nul || exit /b 3
)
move /y "_build_script.tmp" "!_input_file!" > nul || exit /b 3
exit /b 0
:self_extract_func <label> ...
setlocal EnableDelayedExpansion
set "_success=true"
set "_labels= "
for %%l in (%*) do set "_labels=!_labels!%%~l "
call :functions_range _range "%~f0" "!_labels!" || set "_success="
for %%r in (!_range!) do (
call :readline "%~f0" %%r || set "_success="
echo=
echo=
)
if not defined _success exit /b 3
exit /b 0
:Category.unload_info
call :unset_all "Category." "Category_"
exit /b 0
:Category.load
call :Category.load_names
call :Category.build
exit /b 0