Parsing command line

XSharper scripts accept command line parameters. These parameters are declaratively defined, automatically parsed and values assigned to XSharper variables. The same declaration is also used to display script usage information.

	<param name="from" required="true" >Copy from file</param>
	<param name="to" required="true" >Copy to file</param>
	<param switch="overwrite" count="none"	default="false" unspecified="true" 
		tr="trim trimInternal">	
			Specify this parameter if you need files to 
			be overwritten. This text is deliberately verbose 
			to demonstrate wrapping.</param>
	<usage options="default ifHelp ifNoArguments" exitcode="1" />

	<!-- Set overwrite attribute of copy action to always or never -->
	<setattr	actionId="cp" 

	<!-- Do copy -->				
	<set count="0" />
	<copy id="cp" from="${from}" to="${to}">
		<set count="${=(int)$count+1}" />
	<print>${count} files copied</print>


At the beginning three parameters are defined. First two arguments will be assigned to from and to variables, correspondingly. The third parameter, which is optional, is a switch /overwrite which does not have any values following it (count="none"), and sets variable overwrite to true if specified (default is false).

If executed without parameters, because usage options are set to ifNoArguments the script outputs

Copy file utility

Usage: COPY1 from to [parameters]

  from          Copy from file
  to            Copy to file
  /overwrite    Specify this parameter if you need files to be overwritten.
                This text is deliberately verbose to demonstrate wrapping.

and exits with exit code "1". Long text lines are automatically wrapped depending on console window width.

Add GUI to the mix

Command line parameters a great for something that is used often. For less frequently used tools it's too hard to read the documentation first, figure out the right combination of switches through trial and error, deal with escaping and so on.

Fortunately, with XSharper scripts GUI is just one click away (the look is completely customizeable, #/gui-param is just another XSharper script in Library ):

C:\> xsharper #/gui-param COPY1.xsh


Custom usage

While automatic usage generation is useful, it is also possible to replace it with completely custom text instead. For example, the script below displays custom usage text (by adding a
w/o name or switch attributes), and parameter parsing is handled separately (parameters have empty text, and thus not displayed in usage):

	<param tr="trim multiline"><![CDATA[
		Usage: Copy <from-file> <to-file> [/overwrite]
		Copy file from one location to another. By default it skips existing files.
	<param name="from" description="&lt;from-file&gt;" required="true"/>
	<param name="to" description="&lt;to-file&gt;" required="true" />
	<usage options="ifNoArguments ifHelp" exitcode="1" />

Note that using description attribute it's possible to define a user friendly name of a parameter that does not match the variable being set.