![]() |
Calculator Application with Library 2.1.2
A simple library for performing basic arithmetic operations with application
|
Parses and runs the calculator's command-line arguments. More...
Static Public Member Functions | |
| static string | Run (string[] args) |
Parses args as <operation> <a> <b> and returns a line of text describing the result. | |
| static string | RunDemo () |
| Runs a small built-in demonstration that exercises every operation once, used when the application is started with no arguments at all (so a first run always shows something, without waiting on keyboard input). | |
Static Public Attributes | |
| const string | UsageText = "Usage: CalculatorApp <add|subtract|multiply|divide> <a> <b>" |
| The usage line shown when the arguments cannot be parsed. | |
Parses and runs the calculator's command-line arguments.
The parsing logic lives here, in the library, instead of in CalculatorApp/Program.cs, so it can be unit tested directly (see CalculatorLibrary.Tests/CalculatorCliTests.cs) without spawning the console application as a process. Program.cs only calls Run(string[]) or RunDemo and prints the result — it never reads from the console, so it never blocks when the app is launched from a script, a build pipeline or CI.
When you rename this sample into your own project, replace this class with your own argument-parsing entry point, keeping the same "parsing is a library function, not something the console shell does inline" shape.
Definition at line 20 of file CalculatorCli.cs.
|
static |
Parses args as <operation> <a> <b> and returns a line of text describing the result.
| args | Exactly three arguments: the operation name (add, subtract, multiply or divide, case-insensitive) followed by the two integer operands. |
"2 add 2 = 4".| ArgumentException | Thrown when the argument count is wrong, an operand is not a valid 32-bit integer, or the operation name is not recognized. The message always ends with UsageText. |
| DivideByZeroException | Thrown when dividing by zero. |
Definition at line 38 of file CalculatorCli.cs.
|
static |
Runs a small built-in demonstration that exercises every operation once, used when the application is started with no arguments at all (so a first run always shows something, without waiting on keyboard input).
Definition at line 72 of file CalculatorCli.cs.
References CalculatorLibrary.CalculatorCli.UsageText.
|
static |
The usage line shown when the arguments cannot be parsed.
Definition at line 22 of file CalculatorCli.cs.
Referenced by CalculatorLibrary.CalculatorCli.RunDemo().