Monday, 12 May 2014

Regular Expression Command Line Arguments Parser



Here's a simple way to extract a list of switch/flags and their parameters from a command line argument in C#.

Regex regex = new Regex(@"(?< switch> -{1,2}\S*)(?:[=:]?|\s+)(?< value> [^-\s].*?)?(?=\s+[-\/]|$)");

string commands = "--switch value1 --switch2 \"c:\\folder 1\\file1.txt\" -switch-3 value-3 --switch4 -switch5";

List< KeyValuePair< string, string> >  matches = (from match in regex.Matches(commands).Cast< match> ()

select new KeyValuePair< string, string> (match.Groups["switch"].Value, match.Groups["value"].Value)).ToList();

foreach (KeyValuePair< string, string>  _match in matches)
{
Response.Write("
switch:" + _match.Key + "  value:" + _match.Value);
}

You can pass in a command with multiple switches such as "-switch" or "--switch":

--switch1 value1 --switch2 "c:\folder 2\file2.txt" -switch-3 value-3 --switch4 -switch5

 the regex will extract the flags along with any optional values and return a list of key-value pairs.

I had looked at a couple of solutions such as http://www.codeproject.com/Articles/3111/C-NET-Command-Line-Arguments-Parser and http://lukasz-lademann.blogspot.co.uk/2013/01/c-command-line-arguments-parser.html but I think this way is a bit simpler and returns better results.

Also, this way allows optional parameters which can contain file paths, you can test here http://regexhero.net/tester/?id=2f4e252d-c89e-4134-8c7c-65c5186b3338

Thursday, 19 July 2012

Umbraco Examine - Indexing Custom XML Data

I recently ran into a problem with an Umbraco Examine indexer suddenly stopping after trying to index the first node in the content tree.

The only error information I could get was:

Exception information:
Exception type: NullReferenceException
Exception message: Object reference not set to an instance of an object.
at UmbracoExamine.UmbracoContentIndexer.OnGatheringNodeData(IndexingNodeDataEventArgs e)
at Examine.LuceneEngine.Providers.LuceneIndexer.GetDataToIndex(XElement node, String type)




I tried using NodeIndexing & GatheringNodeData handlers to find out more but all I could see was the Examine was trying to index the root node (100 in this case), then looking for Node 1 (Which didn't exist).

Eventually I found out it was due a custom data type (TheFarm - EmbeddedContent) saving content as XML.

The data was being stored as:


    
      
        Node 1 content
        Node 2 content
      
    


The problem seems to be with using "id" as an attribute as it is used in umbraco.config:

<NodeType id="100" parentID="99" level="4" ...



It appears the indexer assumes it is parsing a new node when it reads this.

There are a few options to resolve this problem:
  1. Wrap custom XML data in <![CDATA[
  2. Try using a NodeIndexing handler on the BaseIndexProvider to deal with the XML.
  3. Avoid using id as an attribute in custom XML data.
Option 3 seemed the easiest for me, I updated the  EmbeddedContent source code to use another id attribute.

Wednesday, 18 May 2011

Adobe Wallaby

The world of rich internet content and animation has been in a transitional period for a while.

For years Flash has pretty much been the only viable option (apart from Silverlight), but now other technologies such as Webkit, CSS3 and Canvas are providing alternatives.

It’s no secret Flash has its enemies, but it’s popular with a lot of producers because Adobe have a good suite of development packages that make it accessible to designers and developers.

In order for new options to overshadow Flash, designers will need development software to make producing rich content for Webkit and HTML5 browsers a lot easier.

There are a few out there, Sencha looks quite promising and Adobe has their Edge software in development for authoring HTML5 & Webkit animations.

Another offering is Adobe Wallaby, an application that will convert Flash animations into a Webkit Enabled version. I’ve tried a quick test myself here, it’s a little limited, and its output only seems to run properly in Chrome. It’s still in development, but it looks promising, and it should provide Flash developers an easy way to create content for the iPad and iPhone.

The main problem with these alternative, as far as I can see, is the animations they output all require a Webkit browser to run. In order for a true shift away from Flash I think we need decent development software that will produce standards compliant animations running purely in Canvas.

Monday, 18 April 2011

Umbraco - Missing node in content tree.


I've encountered this problem a few times, a page will go missing from the Umbraco content tree but it will still display on the front end.

It doesn’t show up in the recycle bin but you can find it when you search for it by name, and when you try and open it you get the following server error:

“No Document exists with Version 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx”

It turns out to be due to data being orphaned when the database is updating, you can find out more here .

I followed the advice on the Umbraco issue tracker and deleted the last entry for the affected node in the cmsContentVersion table, it worked for me.

Sunday, 17 April 2011

jsFiddle

I just discovered a site for rapid Javascript development and testing called jsFiddle.

It allows you to write html, css and Javascript code in browser, you can also share your examples with others.

I know its not new, but I thought I would post this so I don't forget about it, looks like it could be a real time saver.


Thursday, 14 April 2011

Websockets


Something I always like about flash was the ability to push data from the server to the client for real-time communication.

For me the down side was it always required some server side technology like Red5 or BlazeDS, and I never had the time to get into Java to do anything too complicated.

Now there is a much better replacement with HTML5 and Websockets, you can push data from the server with a bit of C# and capture it with some simple Javascript.

Websockets aren’t supported by all browsers yet, but I think its quite a handy development, and there are quite a few c# libraries out there to get started.

I had a look at some of them:


They all seem pretty good and fairly easy to use but I had to start with one, Superwebsocket seemed to be the option for me.

One of my main problems with this kind of thing in Flash was it required a separate, standalone server running along side your website.  Superwebsocket’s gives you a few options, you can create a standalone server or you can integrate the code into an existing web project.

Its pretty easy to get started, once you include their DLL’s in your project, most of the code for dealing with client/server commands is included in the global.asax with a little configuration information in the web.config.
I created a fairly simple example here, most other demo’s are basic chatrooms, I thought I’d test to see if it would be feasible to use Websockets for a multiplayer game.

It’s a little jerky, I don’t know if it’s my coding or because I’m integrating the server code into the website, I’m going to try a standalone server version as well to see if it makes a difference.

Tuesday, 12 April 2011

Umbraco login problem - Missing style


I recently ran into a problem when I had to update an Umbraco site that was running fine on a 2008 Server.
To be on the safe side, I took a local copy and set it up on my PC (which runs Vista).

When I tried to run the site, it would work, but none of the images or style sheets would load.  I’ve had similar problems in the past so I thought I would just log into the CMS and republish the site. When I tried this, the welcome page would not display properly either and I could not log in:



After ruling out a problem with permissions or the Client Dependancy cache I checked the request console in firebug. Every css, js and image file being requested was returning a “500 internal server error”.

Basically every request that was not for a .net page was getting a configuration error, so I checked the web.config for anything out of place. After a bit of trial and error I found removing this section fixed it:



 

The site had been setup in Windows Server 2008 and a new mime-type had been added in IIS.

This shouldn’t be a problem viewing the site on Vista as they should both be using the same version of IIS. Also, the file extension had not already been added as a global mime type.

I’m not sure if this is a problem specific to Umbraco sites running on Vista. I’ll have to try and test on a blank site to see.