XSharper can run scripts directly from command line, which is often faster than dealing with files, and replaces interactive mode. There are three special switches:
Evaluate expression from /// until the end of the line. By default it is treated as a piece of C# code:
C:\>xsharper /// Console.Write("Hello,world") Hello,world
Usually it's a good idea to wrap everything into double quotes (so <, > and " characters are not interpreted by command line interpreter).
XSharper command-line processing code replaces ` (back quote) with " in the expression to keep it simpler. In the unfortunate event you do need the ` character, just use "\x60" in C# code, or (char)0x60 in XSharper multi-expressions (there is no escaping there).
C:\>xsharper /// "for (int i=0;i<5;++i) Console.WriteLine(`Hello,world`+i)" Hello,world0 Hello,world1 Hello,world2 Hello,world3 Hello,world4
Print ASCII table of the first 96 printable characters (by generating array of bytes and calling XSharper.Core.Utils.ToHexDump on it):
C:\>xsharper ///p "XS.Utils.ToHexDump(Enumerable.Range(32,128-32).Select(x=>(byte)x).ToArray(),16,32,true)" 00000020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&'()*+,-./ 00000030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 0123456789:;<=>? 00000040 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFGHIJKLMNO 00000050 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVWXYZ[\]^_ 00000060 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefghijklmno 00000070 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvwxyz{|}~⌂
If the expression starts with < it is treated as XML
xsharper /// "<print>Hello, world!</print>"
Finally, if the expression starts with =, it is treated as interpreted multi-expression with relaxed rules
C:\>xsharper /// "=c.writeline(2+(int)"6"+{1,2,'oops'}.length)" 11
Aside of protecting from the wrongdoings of Windows command processor, if the first character of the expression is ", only the string inside the quotes is considered to be a script, and the rest are treated as arguments that can be accessed via ${=$argv[0]}, ${=$argv[1]} and so on. This looks a bit confusing but allows writing simple one liners with arguments.
For example, display the length of file passed as first argument:
C:\>xsharper /// "c.Print(`Length is ${=new FileInfo($argv[0]).Length}`)" C:\Windows\win.ini Length is 569
If the first character is not ", expression is evaluated until the end of the line:
C:\>xsharper /// c.WriteLine("Hello"); c.WriteLine("World"); Hello World
Also, additional assemblies may be specified in the command line as an assembly name, or DLL path. Multiple names may be specified in comma or semicolon separated list.
Load the image specified in the first command line argument and copy it to clipboard:
C:\>xsharper //ref system.windows.forms;system.drawing /// "=System.Windows.Forms.Clipboard.SetImage(system.drawing.Image.FromFile($argv[0]))" C:\bitmap.png
Actually the code above may be shortened via prefixing assembly names with @, which adds assembly names into default namespace (does the right thing for Windows.Forms, System.Diagnostics where namespace matches assembly name):
C:\>xsharper //ref @System.Windows.Forms;@System.Drawing /// "=Clipboard.setImage(Image.fromFile($arg[0]))" C:\bitmap.png
And now, with one more parameter, we produce a complete utility img2clipboard (more about it in Compiling scripts):
C:\>xsharper //genexe img2clipboard.exe //ref @System.Windows.Forms;@System.Drawing /// "=Clipboard.setImage(Image.fromFile($arg[0]))"
Evaluate expression (either C#, or multi-expression if starts with =) that returns value, and write the value with WriteLine .
Read c:\windows\win.ini and print each line within [[ ... ]]:
C:\> xsharper ///p "string.Join(`\n\r`,(from x in c.ReadLines (@`c:\windows\win.ini`) select `[[`+x+`]]`).ToArray())" [[[Mail]]] [[MAPI=1]] ...
Print first 64 bytes of explorer.exe as HEX dump:
C:\>xsharper ///p "=.ToHexDump(.ReadBytes((.Expand('${% WINDIR %}\explorer.exe')),0,64),16,null,true)" 4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 MZ?.........yy.. B8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 ?.......@....... 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 E0 00 00 00 ............a...
Evaluate expression (either C#, or multi-expression if starts with =) that returns value, and dump the value.
xsharper ///# ={1,2,3} (int[]) array[3] { /* #1, 01e6fa8e */ [0] = (int) 1 (0x1) [1] = (int) 2 (0x2) [2] = (int) 3 (0x3) }
In some special cases it may be useful to read script from standard input, instead of the command line. In that case specify - (minus) instead of the expression.
echo "Hello, world" | xsharper ///p -