3

It's a simple task, let me briefly describe it!

I'm supposed to code a command-line tool that takes a file-name as an argument, the file that I'm gonna read consists of lines, each line supposed to be a command to execute, the command is followed by it's appropriate arguments to apply on, to make it clear:

FILE

sum; 1, 2, 3, 4

Output

10

The command-line tool should satisfy those requirements:

1- Easily maintained, developed (more commands might be added in the future) and user-friendly.

2- Command line arguments might be modified and new could be added.

3- Can live as an open-source project, an organised source-tree.

I'm expecting developers to deal with the source-code and fairly understand it.

I'm a newbie in those stuff, I'm kinda new to design patterns so I don't know much, I wanna follow the best practices in developing this program, I really wanna use design patterns if applicable and make my code better and cleaner, so please advise and guide me to write this tool in the best possible way, I don't wanna write dirty code, I wanna write a high-quality code that does what it's intended to and could be easily developed further.

Please advise and feel free to criticize what I've just said.

One last thing, I'll be using C++!

Thanks!

Ahmed Jolani
  • 2,872
  • 2
  • 20
  • 24
  • This isn't so much about "writing a parser" as you think as designing an interpreter for a language, which isn't a very trivial matter. You may wanna look into what a "lexer" and a "parser" are. Flex + Bison are a toolset for C to generate both automagically, for C++ you can look into Boost.Spirit. – TC1 Apr 03 '13 at 19:38
  • 1
    To criticize what you just said: What you need is to hire a programmer, which is not what this website is for. This website, by contrast, is for people who write their own code, have already made a lot of effort, done research, and have come across a specific obstacle, which they demonstrate with a short, minimal, self-contained piece of example code. – Kerrek SB Apr 03 '13 at 19:39
  • I know what a parser is, it's not eligible here as commands could be easily modified and it's not a good way to modify the source-code. – Ahmed Jolani Apr 03 '13 at 19:39
  • For 2), does the main program need to be rebuilt or do you expect plugin-like architecture? – mlt Apr 03 '13 at 19:42
  • rebuilt, there will be a source control. – Ahmed Jolani Apr 03 '13 at 19:43
  • @KerrekSB the idea is I'm learning and this is an obstacle I'm facing, the design stage, I guess it's a real and important step. – Ahmed Jolani Apr 03 '13 at 19:43
  • What about delimeters `;`, `,`? Do those separate arguments or are those passed to commands as is? I mean why not just `"sum this" 1 2 3 4`? Or... how can I pass a comma or have a command ending with semicolon? – mlt Apr 03 '13 at 19:46
  • It's a way in differentiating between the command and the arguments, and also the arguments themselves, if I used a parser adding a new command will require me replacing an old code. – Ahmed Jolani Apr 03 '13 at 19:48
  • I meant could you have all commands as independent programs and just call `system()` from the main one? Otherwise you'd have to parse lines instead of blindly passing them. Keep it simple. – mlt Apr 03 '13 at 19:50
  • @AhmedJolani: That *is* an important step, but this website is not very well suited for that sort of questions, I believe. – Kerrek SB Apr 03 '13 at 20:00
  • @KerrekSB shall it be moved to [programmers.SE](http://programmers.stackexchange.com)? – mlt Apr 03 '13 at 20:16
  • @mlt: Yeah, that sounds better. Or maybe codereview, if there's a bit more substance. – Kerrek SB Apr 03 '13 at 20:33

1 Answers1

0

1- Boost.Program_options is your friend when it comes to parsing command line options.
2- Take a look at the command pattern. Although it is easier to implement in language that has reflection facilities, it is still possible to have a map of "command strings" mapped to function. Please use C++11 facilities for this. i.e. std::function.
3- There is no standard structure for C++ projects. Personally, I use Boost's recommended structure.

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234