https://wiki.visual-prolog.com/index.php?title=Command_Line_Parser&feed=atom&action=historyCommand Line Parser - Revision history2024-03-29T00:08:07ZRevision history for this page on the wikiMediaWiki 1.37.1https://wiki.visual-prolog.com/index.php?title=Command_Line_Parser&diff=4440&oldid=prevThomas Linder Puls: released2017-08-09T20:47:08Z<p>released</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 21:47, 9 August 2017</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;">{{Template:NonReleased}}</del></div></td><td colspan="2" class="diff-side-added"></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2" class="diff-side-added"></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The PFC class <vp>commandLineParser</vp> can be used to deal with command line arguments.</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The PFC class <vp>commandLineParser</vp> can be used to deal with command line arguments.</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
</table>Thomas Linder Pulshttps://wiki.visual-prolog.com/index.php?title=Command_Line_Parser&diff=4304&oldid=prevThomas Linder Puls: command line parser tutorial2016-09-13T11:35:57Z<p>command line parser tutorial</p>
<p><b>New page</b></p><div>{{Template:NonReleased}}<br />
<br />
The PFC class <vp>commandLineParser</vp> can be used to deal with command line arguments.<br />
<br />
==== Example ====<br />
<br />
This little code fragment (from the accompanying demo program) shows the basic operation:<br />
<br />
<vip>class facts<br />
test1 : boolean := false.<br />
test2 : boolean := false.<br />
<br />
clauses<br />
run() :-<br />
CLP = commandLineParser::new(),<br />
CLP:addOption_help("-help"),<br />
CLP:addOption_0("-test1", "Run test1", { () :- test1 := true }),<br />
CLP:addOption_0("-test2", "Run test2", { () :- test2 := true }),<br />
if some(ErrorMessage) = CLP:parse() then<br />
write(ErrorMessage)<br />
else<br />
% command line parsed successfully<br />
run2(CLP)<br />
end if.</vip><br />
<br />
Your program:<br />
* creates an instance of the commandLineParser class<br />
* define the options your program accept<br />
* invoke the parse predicate<br />
* handle errors or continues with normal operation<br />
<br />
The parser cannot only parse the command line it can (and will) also format a help description. If you invoke the program with the help option (which is defined to be "-help" in the demo) you will receive a description like this (as an error text):<br />
<br />
<source lang="text">>cmdLineDemo.exe -help<br />
cmdLineDemo.exe - Command Line Parser Demo<br />
<br />
Usage:<br />
<br />
cmdLineDemo.exe [options] arguments<br />
<br />
Options:<br />
<br />
-help Displays the help message<br />
-test1 Run test1<br />
-test2 Run test2</source><br />
<br />
The program description will by default be taken from the programs version info resource.<br />
<br />
==== Defining and handling options ====<br />
<br />
You define options with the predicates <vp>adoption_0</vp>, <vp..._1</vp>, and <vp..._2</vp>:<br />
<br />
<vip>CLP:addOption_2("-user", "Username and password", "Username", "Password", mkHandler_2("user")),</vip><br />
<br />
You state<br />
* the option<br />
* a description<br />
* names of formal parameters, and<br />
* a callback predicate<br />
<br />
The callback is invoked when the parser parse the corresponding option. The callback receives the actual parameters from the command line as arguments.<br />
<br />
Callbacks will be invoked in the order corresponding to the command line (left-to-right).<br />
<br />
==== Handling regular arguments ====<br />
<br />
Arguments that are not part of options can be handled in two different ways.<br />
<br />
The commandLineParser object have an arguments property, which (after parsing) contains all the arguments (left-to-right) that are not part of options.<br />
<br />
You can also set the onRegular property to a callback that will be invoked when a regular (i.e. non-option) argument is meet (left-to-right).<br />
<br />
Using onRegular and the options callbacks you can deal with the sequence of options:<br />
<br />
<source lang="text">myProgram xxx.txt –log yyy.txt –nolog zzz.txt</source><br />
<br />
Assuming that <vp>–log</vp> and <vp>–nolog</vp> are zero arguments options you will receive the following callback sequence:<br />
<br />
<vip>onRegular("xxx.txt")<br />
onLog()<br />
onRegular("yyy.txt")<br />
onNolog()<br />
onRegular("zzz.txt")</vip><br />
<br />
so logging can be active for yyy.txt, but not for the other two files.<br />
<br />
==== Command files ====<br />
<br />
An argument of the form @<file> (e.g. "@My Commands.txt") will cause the command line parser to read the <file> and parse its contents as command line arguments. After that it will continue parsing the remainder of the original command line.<br />
<br />
==== optionChar ====<br />
<br />
By default options follow the "Unix style" with '-' (minus) as option indicator, but you can set the <vp>optionChar</vp> property to another char (we only recommend '/' (for the "Windows style" options)) which will then be the option indicator:<br />
<br />
<vip>clauses<br />
run() :-<br />
CLP = commandLineParser::new(),<br />
CLP:description := "Demo of '/' options",<br />
CLP:onRegular := mkHandler_1("regular"),<br />
CLP:optionChar := '/',<br />
CLP:addOption_help("/?"),<br />
CLP:addOption_0("/t", "Run in test mode",mkHandler_0("test")),<br />
CLP:addOption_1("/f", "The file to process", "File", mkHandler_1("file")),<br />
CLP:addOption_2("/u", "Username and password", "Username", "Password", mkHandler_2("user")),<br />
...</vip><br />
<br />
Notice that all arguments that start with the optionChar treated as options and will give an error if they are not really an option:<br />
<br />
<source lang="text">>program.exe /myfile.txt<br />
<br />
Unknown option /myfile.txt</source><br />
<br />
==== Sanity Checks ====<br />
<br />
The commandLineParser will raise exceptions if you register the same option twice (also if it have different number of arguments). It will also validate that added options starts with the optionChar. (Changing optionChar and adding options with different option indicators will give bad results).<br />
<br />
==== Double Quotes ====<br />
<br />
In hope of being compatible with '''cmd.exe''' and thus cmd/bat scripts the <vp>commandLineParser</vp> uses [http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx CommandLineToArgvW] to do the actual parsing, especially the handling of double quote ("). <br />
By default the parser splits arguments at the space character, but double quotes can be used to create arguments with space in.<br />
<br />
<source lang="text">program "filename with spaces.txt"</source><br />
<br />
The parser will treat this as a single argument without any quotes: '''filename with spaces.txt'''. I.e. the quotes are removed by the parser.<br />
<br />
For a detailed description of quote handling please read the MSDN documentation [http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx CommandLineToArgvW] including the user comments.<br />
<br />
<br />
[[Category:Tutorials]]</div>Thomas Linder Puls