How do I best handle arguments with modifiers in a console program? - c#

I'm writing an argument parser for a console program, but I'm certain that my current implementation is not the best way to do this. Since this is a common problem, is there a standard/better/best way to pass multiple arguments with optional modifiers to a console program?
private static void Main(params string[] args)
{
for (int i = 0; i < args.Length; i++)
{
// Is there a valid argument?
if (args[i] == "/r")
{
Console.WriteLine("Argument: /r");
// Is there a successive argument?
if (i + 1 < args.Length)
{
// Is the successive argument an argument or a modifier?
if (args[i + 1].IndexOf("/", StringComparison.CurrentCultureIgnoreCase) == -1)
{
Console.WriteLine("Argument: /r " + args[i + 1]);
}
}
}
}
}
The main problem I see with this implementation is that I'd have to rewrite the last two if blocks for each argument and modifier, which would become unwieldy for a large program, I think.

Related

How to check if two strings length are equal in c#?

I'm new in c# and unity and Wondering how could I write this code to check if two string lengths are equal.
with this code unity system shows this error:
error CS1061: Type char' does not contain a definition forLength' and no extension method Length' of typechar' could be found.
for (int i = 0; i < Answers.Length; i++)
{
if (GetAnswer[i].Length == Answers[i].Length)
{
//Do something
}
}
if (yourString.Length == yourOtherString.Length)
{
//dosomething
}
should check if a string is equals in length to another
The problem with your code is that a string is an array of char so it calculate the length of the char (Which do not exist)
for (int i = 0; i < Answers.Length+1; i++)
{
if (GetAnswer.Length == Answers.Length)
{
//Do something
}
}
You also need to increment the value of the for to get the correct length otherwise the Answers.Length will always be short of 1

The proper way to use parameters/arguments in c#?

I have an app that gets 3 main parameters: sum, substract, print. If one of this parameters is given the app calls the methods: sum for sum, substract for substract, print for print.
Calling with parameters all examples:
myapp.exe sum 1 2
myapp.exe substract 3
myapp.exe print whatever
This is my actual code which doesn't work
static void Main(string[] args)
{
Program package = new Program();
if (args[0] == #"sum")
{
package.sum(args[1], args[2]);
}
else if (args[0] == #"substract")
{
package.substract(args[1]);
}
else if (args[0] == #"print")
{
package.print(args[1]);
}
else
Console.Write("Invalid Parameters");
}
But this is the error I get when debugging:
A first chance exception of type 'System.IndexOutOfRangeException' occurred in myapp.exe
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in myapp.exe
Additional information: Index was outside the bounds of the array.
What am I doing wrong? What is the best way to work with parameters ?
What am I doing wrong?
You are assuming that the program is always called with the proper number of parameters. Specifically, that
At least one parameter is present
When the initial parameter is sum or print, two parameters are present
When the initial parameter is add, three parameters are present
This is not always correct, depending on what the user enters. You need to add code checking for the length of the args array before referencing its elements:
if (args.Length < 2) {
Console.Error.Write("Invalid Parameters");
return;
}
if (args[0] == #"sum" && args.Length == 3) {
...
}
... // The rest of your code can assume that there's at least two args
The answer above is great but I feel like there is a little more to this answer than just the number of variables.
I find that the following type of solution works best for me when in need of a somewhat robust argument solution:
if (args.Length > 0)
{
if (args.Contains<string>("--help"))
{
Console.WriteLine("appname [--debug] [--bootstrap <bootstrap name>]");
Console.WriteLine(" --debug Runs \"appname\" in debug mode.")
Console.WriteLine(" --bootstrap If no bootstrap name is provided the default \"AppName v1.2\" will be used.")
Console.WriteLine("")
}
//simple single argument passed to application
if (args.Contains<string>("--debug"))
{
DEBUG_ON = true;
Console.WriteLine("Found argument: --debug");
}
//more complex relational argument passed to application
if (args.Contains<string>("--bootstrap"))
{
int bootstrapLoc = Array.IndexOf<string>(args, "--bootstrap");
//always check that there is one more argument in the array
if(args.Length > bootstrapLoc + 1)
{
string bootstrapArg = args[bootstrapLoc + 1];
//this is where you would do your error checking against the second argument, ex: determine that it is what is expected
if (bootstrapArg != null && bootstrapArg != "")
{
this.MongoDBInstallName = bootstrapArg;
Console.WriteLine("Using the passed bootstrap arg: " + bootstrapArg);
}
}
}
}
There are definitely better ways to do this and this by no means is fool proof but I just wanted to add some more detail to this question.

Why am I getting this Error in C# Compiler "Unassigned Local Variable" [duplicate]

This question already has answers here:
c# switch problem
(8 answers)
Closed 9 years ago.
I am getting the following error. Use of unassigned local variable markduplicate. I don't understand why? This program finds a duplicate in an array. I been trying to figure it out and I feel like im so close. Thanks for the help.
using System;
class duplicate
{
static void Main()
{
const int Array_Size = 5;
int [] number = new int [Array_Size];
int i;
for ( i = 0; i < Array_Size; i++)
{
Console.Write("Element " + i + ": ");
number[i] = Int32.Parse(Console.ReadLine());
if (number[i] < 9 || number[i] > 101)
{
Console.WriteLine("Enter Number between 10 - 100");
number[i] = Int32.Parse(Console.ReadLine());
}
}
bool markduplicate;
for (i = 0; i < Array_Size; i++)
{
for (int j = 0; j < Array_Size; j++)
{
if (i != j)
{
if (number[j] == number[i])
{
markduplicate = true;
}
}
if (markduplicate != true)
{
Console.WriteLine("Element " + i + " " + number[i]);
}
}
}
}
}
This is because the C#'s code analyzer detected that there are paths through you program when the markduplicate's value would be referenced before any assignments to it are made. Specifically, this is going to happen during the very first iteration of the nested loop, when both i and j are zero: the if (i != j) block containing the assignment is not going to execute, so the value of the markduplicate is going to get retrieved in the very next if statement.
To fix this problem, change
bool markduplicate;
to
bool markduplicate = false;
in the declaration.
The compiler thinks it is possible for you to hit the if (markduplicate != true) line before markduplicate has been set.
If you think the compiler is wrong, give it a value when declaring e.g. bool markduplicate = true;. If you analyze and think the compiler is correct, adjust you code accordingly.
Also: if (markduplicate != true) is considered poor style. Use if(!markduplicate) instead.
You've declared the boolean variable markduplicate but you haven't given it an initial value - the compiler doesn't know if it should be initially true or false.
In this case it's clear you want it to be initially false, so put this:
bool markduplicate = false;
it will now compiled.
In your code, (i != j) is false before it is true and therefore you are checking the value of your variable which has had nothing assigned to it.
You need to either assign a value to markduplicate at its declaration or you need to make sure that any path that leads to a conditional check on its value has a value assigned to it first.
You have to assign markdefault to either true or false in your declaration. Otherwise, if number [j] != number [i], then markdefault will not be assigned and if markduplicate != true cannot be evaluated.

Adding custom variables to the end of application path when executed in c#

How would I read custom variables end of my application path? I am not sure exactly what they are called, but for example in in the shortcut path of Google Chrome you can add settings to the end of the path such as:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --Custom-setting"
How would I read these arguments after my application has started?
They are arguments that you are passing to the application. For example take the following application:
static void Main(string[] args)
{
if (args == null)
{
Console.WriteLine("args is null"); // Check for null array
}
else
{
Console.Write("args length is ");
Console.WriteLine(args.Length); // Write array length
for (int i = 0; i < args.Length; i++) // Loop through array
{
string argument = args[i];
Console.Write("args index ");
Console.Write(i); // Write index
Console.Write(" is [");
Console.Write(argument); // Write string
Console.WriteLine("]");
}
}
Console.ReadLine();
If you ran the following from the command line:
"C:\ConsoleApplication1.exe" a b c
The program would display:
args length is 3
args index 0 is [a]
args index 1 is [b]
args index 2 is [c]
http://msdn.microsoft.com/en-us/library/acy3edy3.aspx
http://msdn.microsoft.com/en-us/library/cb20e19t.aspx
These values will be contained in the args array in the Main() method which serves as the entry point of your application.
static void Main(string[] args) { }
Command-Line Arguments
Main() and Command-Line Arguments
They are called command line arguments. Although you can parse them manually directly from your main() program entry point, there are several helper libraries which can make life even easier for you and your users, such as this one here and here.

What's the best way to do a backwards loop in C/C#/C++?

I need to move backwards through an array, so I have code like this:
for (int i = myArray.Length - 1; i >= 0; i--)
{
// Do something
myArray[i] = 42;
}
Is there a better way of doing this?
Update: I was hoping that maybe C# had some built-in mechanism for this like:
foreachbackwards (int i in myArray)
{
// so easy
}
While admittedly a bit obscure, I would say that the most typographically pleasing way of doing this is
for (int i = myArray.Length; i --> 0; )
{
//do something
}
In C++ you basicially have the choice between iterating using iterators, or indices.
Depending on whether you have a plain array, or a std::vector, you use different techniques.
Using std::vector
Using iterators
C++ allows you to do this using std::reverse_iterator:
for(std::vector<T>::reverse_iterator it = v.rbegin(); it != v.rend(); ++it) {
/* std::cout << *it; ... */
}
Using indices
The unsigned integral type returned by `std::vector::size` is *not* always `std::size_t`. It can be greater or less. This is crucial for the loop to work.
for(std::vector<int>::size_type i = someVector.size() - 1;
i != (std::vector<int>::size_type) -1; i--) {
/* std::cout << someVector[i]; ... */
}
It works, since unsigned integral types values are defined by means of modulo their count of bits. Thus, if you are setting -N, you end up at (2 ^ BIT_SIZE) -N
Using Arrays
Using iterators
We are using `std::reverse_iterator` to do the iterating.
for(std::reverse_iterator<element_type*> it(a + sizeof a / sizeof *a), itb(a);
it != itb;
++it) {
/* std::cout << *it; .... */
}
Using indices
We can safely use `std::size_t` here, as opposed to above, since `sizeof` always returns `std::size_t` by definition.
for(std::size_t i = (sizeof a / sizeof *a) - 1; i != (std::size_t) -1; i--) {
/* std::cout << a[i]; ... */
}
Avoiding pitfalls with sizeof applied to pointers
Actually the above way of determining the size of an array sucks. If a is actually a pointer instead of an array (which happens quite often, and beginners will confuse it), it will silently fail. A better way is to use the following, which will fail at compile time, if given a pointer:
template<typename T, std::size_t N> char (& array_size(T(&)[N]) )[N];
It works by getting the size of the passed array first, and then declaring to return a reference to an array of type char of the same size. char is defined to have sizeof of: 1. So the returned array will have a sizeof of: N * 1, which is what we are looking for, with only compile time evaluation and zero runtime overhead.
Instead of doing
(sizeof a / sizeof *a)
Change your code so that it now does
(sizeof array_size(a))
I would always prefer clear code against 'typographically pleasing' code.
Thus, I would always use :
for (int i = myArray.Length - 1; i >= 0; i--)
{
// Do something ...
}
You can consider it as the standard way to loop backwards.
Just my two cents...
In C#, using Visual Studio 2005 or later, type 'forr' and hit [TAB] [TAB]. This will expand to a for loop that goes backwards through a collection.
It's so easy to get wrong (at least for me), that I thought putting this snippet in would be a good idea.
That said, I like Array.Reverse() / Enumerable.Reverse() and then iterate forwards better - they more clearly state intent.
In C# using Linq:
foreach(var item in myArray.Reverse())
{
// do something
}
That's definitely the best way for any array whose length is a signed integral type. For arrays whose lengths are an unsigned integral type (e.g. an std::vector in C++), then you need to modify the end condition slightly:
for(size_t i = myArray.size() - 1; i != (size_t)-1; i--)
// blah
If you just said i >= 0, this is always true for an unsigned integer, so the loop will be an infinite loop.
Looks good to me. If the indexer was unsigned (uint etc), you might have to take that into account. Call me lazy, but in that (unsigned) case, I might just use a counter-variable:
uint pos = arr.Length;
for(uint i = 0; i < arr.Length ; i++)
{
arr[--pos] = 42;
}
(actually, even here you'd need to be careful of cases like arr.Length = uint.MaxValue... maybe a != somewhere... of course, that is a very unlikely case!)
The best way to do that in C++ is probably to use iterator (or better, range) adaptors, which will lazily transform the sequence as it is being traversed.
Basically,
vector<value_type> range;
foreach(value_type v, range | reversed)
cout << v;
Displays the range "range" (here, it's empty, but i'm fairly sure you can add elements yourself) in reverse order.
Of course simply iterating the range is not much use, but passing that new range to algorithms and stuff is pretty cool.
This mechanism can also be used for much more powerful uses:
range | transformed(f) | filtered(p) | reversed
Will lazily compute the range "range", where function "f" is applied to all elements, elements for which "p" is not true are removed, and finally the resulting range is reversed.
Pipe syntax is the most readable IMO, given it's infix.
The Boost.Range library update pending review implements this, but it's pretty simple to do it yourself also. It's even more cool with a lambda DSEL to generate the function f and the predicate p in-line.
In C I like to do this:
int i = myArray.Length;
while (i--) {
myArray[i] = 42;
}
C# example added by MusiGenesis:
{int i = myArray.Length; while (i-- > 0)
{
myArray[i] = 42;
}}
I prefer a while loop. It's more clear to me than decrementing i in the condition of a for loop
int i = arrayLength;
while(i)
{
i--;
//do something with array[i]
}
i do this
if (list.Count > 0)
for (size_t i = list.Count - 1; ; i--)
{
//do your thing
if (i == 0) //for preventing unsigned wrap
break;
}
but for some reason visual studio 2019 gets angry and warns me "ill-defined loop" or something.. it doesnt trust me
edit: you can remove "i >= 0" from "for (size_t i = list.Count - 1; i >= 0; i--)" .. its unnecessary
I'm going to try answering my own question here, but I don't really like this, either:
for (int i = 0; i < myArray.Length; i++)
{
int iBackwards = myArray.Length - 1 - i; // ugh
myArray[iBackwards] = 666;
}
I'd use the code in the original question, but if you really wanted to use foreach and have an integer index in C#:
foreach (int i in Enumerable.Range(0, myArray.Length).Reverse())
{
myArray[i] = 42;
}
// this is how I always do it
for (i = n; --i >= 0;){
...
}
For C++:
As mentioned by others, when possible (i.e. when you only want each element at a time) it is strongly preferable to use iterators to both be explicit and avoid common pitfalls. Modern C++ has a more concise syntax for that with auto:
std::vector<int> vec = {1,2,3,4};
for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
std::cout<<*it<<" ";
}
prints 4 3 2 1 .
You can also modify the value during the loop:
std::vector<int> vec = {1,2,3,4};
for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
*it = *it + 10;
std::cout<<*it<<" ";
}
leading to 14 13 12 11 being printed and {11, 12, 13, 14} being in the std::vector afterwards.
If you don't plan on modifying the value during the loop, you should make sure that you get an error when you try to do that by accident, similarly to how one might write for(const auto& element : vec). This is possible like this:
std::vector<int> vec = {1,2,3,4};
for (auto it = vec.crbegin(); it != vec.crend(); ++it) { // used crbegin()/crend() here...
*it = *it + 10; // ... so that this is a compile-time error
std::cout<<*it<<" ";
}
The compiler error in this case for me is:
/tmp/main.cpp:20:9: error: assignment of read-only location ‘it.std::reverse_iterator<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > >::operator*()’
20 | *it = *it + 10;
| ~~~~^~~~~~~~~~
Also note that you should make sure not to use different iterator types together:
std::vector<int> vec = {1,2,3,4};
for (auto it = vec.rbegin(); it != vec.end(); ++it) { // mixed rbegin() and end()
std::cout<<*it<<" ";
}
leads to the verbose error:
/tmp/main.cpp: In function ‘int main()’:
/tmp/main.cpp:19:33: error: no match for ‘operator!=’ (operand types are ‘std::reverse_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >’ and ‘std::vector<int>::iterator’ {aka ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’})
19 | for (auto it = vec.rbegin(); it != vec.end(); ++it) {
| ~~ ^~ ~~~~~~~~~
| | |
| | std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}
| std::reverse_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >
If you have C-style arrays on the stack, you can do things like this:
int vec[] = {1,2,3,4};
for (auto it = std::crbegin(vec); it != std::crend(vec); ++it) {
std::cout<<*it<<" ";
}
If you really need the index, consider the following options:
check the range, then work with signed values, e.g.:
void loop_reverse(std::vector<int>& vec) {
if (vec.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
throw std::invalid_argument("Input too large");
}
const int sz = static_cast<int>(vec.size());
for(int i=sz-1; i >= 0; --i) {
// do something with i
}
}
Work with unsigned values, be careful, and add comments, e.g.:
void loop_reverse2(std::vector<int>& vec) {
for(size_t i=vec.size(); i-- > 0;) { // reverse indices from N-1 to 0
// do something with i
}
}
calculate the actual index separately, e.g.:
void loop_reverse3(std::vector<int>& vec) {
for(size_t offset=0; offset < vec.size(); ++offset) {
const size_t i = vec.size()-1-offset; // reverse indices from N-1 to 0
// do something with i
}
}
If you use C++ and want to use size_t, not int,
for (size_t i = yourVector.size(); i--;) {
// i is the index.
}
(Note that -1 is interpreted as a large positive number if it's size_t, thus a typical for-loop such as for (int i = yourVector.size()-1; i>=0; --i) doesn't work if size_t is used instead of int.)
Not that it matters after 13+ years but just for educational purposes and a bit of trivial learning;
The original code was;
for (int i = myArray.Length - 1; i >= 0; i--)
{
// Do something
myArray[i] = 42;
}
You don't really need to test 'i' again being greater or equal to zero since you simply need to only produce a 'false' result to terminate the loop. Therefore, you can simple do this where you are only testing 'i' itself if it is true or false since it will be (implicitly) false when it hits zero.;
for (int i = myArray.Length - 1; i; i--)
{
// Do something
myArray[i] = 42;
}
Like I stated, it doesn't really matter, but it is just interesting to understand the mechanics of what is going on inside the for() loop.
NOTE: This post ended up being far more detailed and therefore off topic, I apologize.
That being said my peers read it and believe it is valuable 'somewhere'. This thread is not the place. I would appreciate your feedback on where this should go (I am new to the site).
Anyway this is the C# version in .NET 3.5 which is amazing in that it works on any collection type using the defined semantics. This is a default measure (reuse!) not performance or CPU cycle minimization in most common dev scenario although that never seems to be what happens in the real world (premature optimization).
*** Extension method working over any collection type and taking an action delegate expecting a single value of the type, all executed over each item in reverse **
Requres 3.5:
public static void PerformOverReversed<T>(this IEnumerable<T> sequenceToReverse, Action<T> doForEachReversed)
{
foreach (var contextItem in sequenceToReverse.Reverse())
doForEachReversed(contextItem);
}
Older .NET versions or do you want to understand Linq internals better? Read on.. Or not..
ASSUMPTION: In the .NET type system the Array type inherits from the IEnumerable interface (not the generic IEnumerable only IEnumerable).
This is all you need to iterate from beginning to end, however you want to move in the opposite direction. As IEnumerable works on Array of type 'object' any type is valid,
CRITICAL MEASURE: We assume if you can process any sequence in reverse order that is 'better' then only being able to do it on integers.
Solution a for .NET CLR 2.0-3.0:
Description: We will accept any IEnumerable implementing instance with the mandate that each instance it contains is of the same type. So if we recieve an array the entire array contains instances of type X. If any other instances are of a type !=X an exception is thrown:
A singleton service:
public class ReverserService
{
private ReverserService() { }
/// <summary>
/// Most importantly uses yield command for efficiency
/// </summary>
/// <param name="enumerableInstance"></param>
/// <returns></returns>
public static IEnumerable ToReveresed(IEnumerable enumerableInstance)
{
if (enumerableInstance == null)
{
throw new ArgumentNullException("enumerableInstance");
}
// First we need to move forwarad and create a temp
// copy of a type that allows us to move backwards
// We can use ArrayList for this as the concrete
// type
IList reversedEnumerable = new ArrayList();
IEnumerator tempEnumerator = enumerableInstance.GetEnumerator();
while (tempEnumerator.MoveNext())
{
reversedEnumerable.Add(tempEnumerator.Current);
}
// Now we do the standard reverse over this using yield to return
// the result
// NOTE: This is an immutable result by design. That is
// a design goal for this simple question as well as most other set related
// requirements, which is why Linq results are immutable for example
// In fact this is foundational code to understand Linq
for (var i = reversedEnumerable.Count - 1; i >= 0; i--)
{
yield return reversedEnumerable[i];
}
}
}
public static class ExtensionMethods
{
public static IEnumerable ToReveresed(this IEnumerable enumerableInstance)
{
return ReverserService.ToReveresed(enumerableInstance);
}
}
[TestFixture]
public class Testing123
{
/// <summary>
/// .NET 1.1 CLR
/// </summary>
[Test]
public void Tester_fornet_1_dot_1()
{
const int initialSize = 1000;
// Create the baseline data
int[] myArray = new int[initialSize];
for (var i = 0; i < initialSize; i++)
{
myArray[i] = i + 1;
}
IEnumerable _revered = ReverserService.ToReveresed(myArray);
Assert.IsTrue(TestAndGetResult(_revered).Equals(1000));
}
[Test]
public void tester_why_this_is_good()
{
ArrayList names = new ArrayList();
names.Add("Jim");
names.Add("Bob");
names.Add("Eric");
names.Add("Sam");
IEnumerable _revered = ReverserService.ToReveresed(names);
Assert.IsTrue(TestAndGetResult(_revered).Equals("Sam"));
}
[Test]
public void tester_extension_method()
{
// Extension Methods No Linq (Linq does this for you as I will show)
var enumerableOfInt = Enumerable.Range(1, 1000);
// Use Extension Method - which simply wraps older clr code
IEnumerable _revered = enumerableOfInt.ToReveresed();
Assert.IsTrue(TestAndGetResult(_revered).Equals(1000));
}
[Test]
public void tester_linq_3_dot_5_clr()
{
// Extension Methods No Linq (Linq does this for you as I will show)
IEnumerable enumerableOfInt = Enumerable.Range(1, 1000);
// Reverse is Linq (which is are extension methods off IEnumerable<T>
// Note you must case IEnumerable (non generic) using OfType or Cast
IEnumerable _revered = enumerableOfInt.Cast<int>().Reverse();
Assert.IsTrue(TestAndGetResult(_revered).Equals(1000));
}
[Test]
public void tester_final_and_recommended_colution()
{
var enumerableOfInt = Enumerable.Range(1, 1000);
enumerableOfInt.PerformOverReversed(i => Debug.WriteLine(i));
}
private static object TestAndGetResult(IEnumerable enumerableIn)
{
// IEnumerable x = ReverserService.ToReveresed(names);
Assert.IsTrue(enumerableIn != null);
IEnumerator _test = enumerableIn.GetEnumerator();
// Move to first
Assert.IsTrue(_test.MoveNext());
return _test.Current;
}
}

Categories