Design questions - creating a command line interface and controlling privilege level

Jun 26, 2017 at 12:17pm
Hello,

I am writing a console application which needs to print certain messages while performing calculations and have the ability to respond to some command-line input from user. The privilege level and the messages are loaded from csv / excel files.

Q1:
I would like to add privilege level control for what each user sees

Example:
highest level of privilege: sees __FUNCTION__ names for each function processing
+ messages which describe intermediate calculation steps

lowest level of privilege: sees only a handful of messages about what calculation is being performed / does not see description of intermediate calculation steps

Question: what is the best way to implement this
?

What I have thought of so far:
I was thinking to create a print_messages namespace which looks like:
print_messages::print_message(int message_id,int privilege_level)
This is not ideal however, as the calculation involves quite a lot of steps and passing int privilege_level around each function is fairly cumbersome and verbose. I am not sure if declaring it as a global variable is considered good designed practice / introduces issues in changing access. I would also like to keep my main as minimalistic as possible because the program has a huge number of parameters and declaring stuff in main / global might make it unreadable / hard to maintain.

Q2:
I would like to add a simple command-line interface which can call functions

What I have thought of so far:
create a class Console_commands which matches std::cin input from user to function pointers of what I want the user to run. Again I am not sure if this is the right design practice to implement this sort of thing. I want to design it properly since it will require quite a lot of modification in the process and maintenance later

Question: what is the good design practice to implement this?
Last edited on Jun 26, 2017 at 12:19pm
Jun 26, 2017 at 12:45pm
Yes, global vars are considered bad practice.
A better alternative would be a AppSettings class as a singleton.
How do you set the privilege level ?
Jun 26, 2017 at 12:51pm
just a private member of the class as an int

question then is - what is the best way to pass this around?

My functions look smth like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
Load_batch_of_settings_from_file_1 -- vector
Load_batch_of_settings_from_file_2 -- vector
Load_batch_of_settings_from_file_3 -- vector

--> 
foreach batch of settings (run some transformation of these)

-->
use these transformed settings to feed into main calculation functions
within here there are multiple levels of loops and often I have stuff like:

func_process_whole_loop (batch of settings)
func_process_single_iteration([i]th index of the batch of settings)


The problem is passing this AppSettings around - a lot of the messages are printed in
func_process_single_iteration([i]th index of the batch of settings) type of functions which are several levels nested inside the main processing function
Jun 26, 2017 at 3:22pm
You don't pass the singleton around. You only include the header file where you need to use it.
Like so:
AppSettings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class AppSettings
{
public:
  static AppSettings& getInstance()
  {
    static AppSettings settings;
    return settings;
  }
  int getPrivilege() {return privilege;}
  void setPrivilege(int newPrivilege) { privilege = newPrivilege;}
private:
  int privilege;
  AppSettings(): privilege(0) {};
  AppSettings(const AppSettings& rhs) = delete;
  AppSettings& operator = (AppSettings& rhs) = delete;
};


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "AppSettings.h"

int main()
{
  int priv = AppSettings::getInstance().getPrivilege();
  
  // use priv
}


Jun 26, 2017 at 9:10pm
perfect, thanks a lot Thomas1965!, got it
Topic archived. No new replies allowed.