Commandline parameters, C# and stdout - c#

I'm supposed to write a command line tool in C#. Problem is, I'm completely new to it and have to read up on a lot of stuff. The tool has to accept several parameters with a syntax I have no idea of what it does. It goes like this:
tool.exe \path\data.log /lastrun:file1.txt >file2.txt
Is that /lastrun:... valid markup?
I know that >file2.txt has something to do with output and stdout, but I barely find any info for dummies. Does it write a text file?
The tool is supposed to output data on stdout, which is meant to be read again and possibly processed with further console commands. How can one reference the output?
I have practically no experience with command line tools. I would appreciate if anyone could either drop me some smart words I could look up, links or simply explains me what is going on here.

You are the one to decide the format of the command line parameters (what you call "markup").
It is entirely up to you whether it is valid or not.
You need to parse the passed in arguments - see Main() and Command-Line Arguments (C# Programming Guide) on MSDN for details. Many people use a command line parsing library (there are many - search and find one you like, perhaps the one with the best documentation).
As for > - I suggest you read about command redirection (article about XP, but still valid).

Outputting data on stdout is easy. Just write to the Console class. If you want to read in you could use the static read methods on the console class as well, though depending on the type of data you are sending you may want to look into pipes. Here is another post Standard Input & Output in .NET asking the same question.
As far as console input format, like was mentioned, that's up to you!

Related

is there a "Data Conversion Object" principle/pattern?

I recently had a question regarding String Object with fixed length C# .
(Please read this question first)
Some of the answers, which were given, pointed out that my design might be flawed.
Since the last question was about Strings with a fixed length this one is about the underlying principle. This question might be a little bit long so pleas bear with me.
Requirements:
I have a plain textfile with values in it with a specified fixed length. The standard for this textfiles is from the 90's. I have to create such a file.
A File may contain 1-60 Rows.
There are 10 different types of Rows.
A Row has between 10-40 values.
A Row is specified like this:
Back in the 90's there was an application which created those files placed it on a Server and the server then read the File and did something with it like writing it to the database or informing somebody that something went wrong etc.
This application isn't usable anymore due to recent legal changes.
Suggested design
The new Application that is in its place doesn't provide any data in the form of an export but it has a database with the values inside. I have the responsibility to write a converter. So I have to get the data and write an exported text file. The Data is only send and never received !
Question
Since a A DTO's only purpose is to transfer state, and should have no behavior(POCO vs DTO)
Is there something like a "Data Conversion Object" which has the purpose of converting data which is transfered ? Is there a design pattern which is applicable ?
I recently designed a solution for a similar problem, though my solution was in SAS language, which is not Object-Oriented. But, to me it seems that the problem is pretty much the same. Now, lets dissect the problem:
The problem:
There are some plain text files.
These files have specification, about the layout, fields, types etc.
These files need to be converted to some other format.
Solution (Objected-Oriented):
I would define three classes, PlainTextFile, Specification, Output, and a Reader Class.
Specification: Contractor takes an specification (probably it is stored in a file or so), and parses that into an Specification object.
PlainTextFile: This can be handle to a text file, or a wrapper around the handle if some other feature is added to it. I prefer the second option.
Output: This is the output you would like to produce.
Reader: It takes two inputs, PlainTextFile and Specification. Uses Specification to read and parse the PlainTextFile and write the output in the Output object/format.
Now, the output can be the final step or not. I suggest, that the Reader do only this much. It you want to write the output to a database, or send it somewhere, create another class to do this.
Remember, I don't know what the name of this pattern. Actually, I don't think that matter much. For me, this method solved a problem that existed in the company for a decade and it integrated two of the most used systems there.

Running multiple RegEx through log files

I need to build a program that can read through live logging, identify lines, and parse data out of the lines.
Problems I'm facing:
The log output changes from version to version, of the logger.
There are about 500 different types of lines that the logger can output.
There is no easy way to make this, as far as I can tell. I was wondering if there was a specific way of doing stuff like this, as it seems pretty overwhelming?
My current solution is to read the logs and run all my Regexes through each line of the logs to test if it matches.
I have an array with a type I call DataReader, and each contains multiple RegEx formats to read different versions of the logger's lines.
First it tests if the DataReader can read it, using:
bool canUse(String text);
If it returns false, it tries another DataReader until it returns true.
If canUse returns true, it will then construct the data structure using
CompiledLogData constructData(String text);
I am not asking for someone to code this; I'm just asking if this is the right way, or if there is a better way, perhaps a more optimal way for this type of a thing? I am sure someone has worked with a situation like this before? :)
Hope someone can help, thanks.
This is not necessarily a solution, but what i prefer to do in situations like these, is use the RegEx statements on the whole thing... that way you can programmatically build your lines based on reading the entire log into memory. As far as performance is concerned though, it would be the same as yours, just reverse direction (you're doing #for each Line, Do each regexp#, i do #for entire log, Do each regexp#).
What i like about the way i do it is that i can reduce the size of the log on each match by placing the match in a separate var and replacing it in the log with nothing, then move onto next regexp. Different way about it i guess...
If you are bound to C# this might be a good approach. At least I do not know any better.
If you just need to transform the data into a different output format (e.g. into a file), you might use an awk script (awk was made for tasks like this). This skript might read your log messages from stdin and writes the transformed data to stdout. So you can pipe your log messages to your awk script and get the transformed data back from stdout for further processing.
Reading the log and tranforming the data are seperate applications and you have more flexibility in the usage of the tools.
Especially when the regular expressions change oftenly, you might speed up your work by using a script language to avoid the additional compile-step you need in c#.

command parser/interpreter [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best way to parse command line arguments in C#?
I'm looking for a library that can provide easy command parsing. Somthing similar to command line parsers would be fine. The trouble with a command line parser is getting the actual command and paring it to the options easily.
For instance I may want to turn on debugging. To do so I would want to do something like this.
debug on -f "c:\logs\debug file.txt"
If I wanted to turn on optimization I could do this.
optimize on
or
optimize off
While I might be able to get a standard command line parser to accomplish this, there would need to be a bit of preprocessing before actually using a standard command line parser. I'm really just looking for an elegant solution that won't require tons of debugging.
On a side note, has anyone any recommendation using python in .NET?
Python has one built in called argparse Python argparser
And for C# you can use: C# Argparse
If I needed to write one myself, I'd start out by writing a stack-based language interpreter, and then it'll be really easy to parse options. You'll have written half the grammar just by writing the lexer.
Python is one of those things that when you add it to anything, it instantly becomes awesome. So you can only imagine what using Python with .NET would be like. Coming from personal experience, you can do a lot of work very quickly using IronPython. (Oh, and you can use the TPL (Task Parallelism Library) too.
Have fun!

Full Old-School TUI Command Line Application in C#

I have coded a high performance real-time data data processor, and I have spent WAY too much time trying to make the GUI not to freeze, be responsive, etc. (using async invoke and other gui best practices).
However, my team and I decided to move to a plain old simple and efficient Text-User-Interface and get rid of all the bells and whistles for more efficiency and performance.
So I would like to create a console app that is similar to command lines apps like sql or telnet.
I know how to manage basic arguments and console read/write, but I was wondering how these apps work. Is there something specific about it ? Just a matter of curiosity. Can I display something like:
MYAPP>Run command1 -arg1 -arg2
>Success !
MYAPP>Show Log
>...
MYAPP>Run command2 -arg1 -arg2
etc.
is the "MYAPP>" prefix just something to be parsed plainly or is there a smarter way to do that ?
EDIT
I have Stumbled upon this TUI library (pic below). But it not complete and not maintained anymore.
If somebody know of something similar I welcome any suggestion.
EDIT 2
curse library seems interesting too, as stated in this topic
The ConsoleDraw library could be of used here.
It's similar to the TUI library you mentioned, but with more controls and still maintained. It is possible to add your own custom controls if you needed them also.

Library/Framework for Parsing Output from Commandline (C#)?

My application needs to:
Run an external command-line application
Capture the standard output/error during execution
Parse the data from the output/error and use it for status display in the main app
While it's not hard to do it, it seems to me it's a very common task, and I was wondering if there's a library or framework simplifying this task in C#.
Edit: Here's a "Mono.Options inspired" example of the kind of thing I'm looking for:
ProcessWithParser proc= new ProcessWithParser(filename, arguments);
proc.AddWatch("percent=??", v => this.percent = Convert.ToInt32(v));
proc.Start();
In the "concept" above, it would locate "percent=" in the output, read the next two characters as a string, and call the delegate using this string as a parameter.
The base class library provides the tools for this already - there isn't much a framework would really need to add to it.
Redirection of the process standard input and output streams is trivial. RegEx and using streams makes parsing this fairly easy, as well.
The framework pretty much does what any (flexible) custom library would normally do in other languages.

Categories

Resources