PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/init/arg-parser.js

https://gitlab.com/willko/net-proj1
JavaScript | 149 lines | 119 code | 10 blank | 20 comment | 12 complexity | cc3c0ee51730072055e729c4678e2ad4 MD5 | raw file
  1. /********************************
  2. *********************************
  3. Program: Project One (CS4310)
  4. Author: William Olson
  5. Date: 11/04/2015
  6. File: arg-parser.js
  7. This file is a helper utility
  8. for reading arguments from the
  9. console, & reading data in from
  10. the topology file.
  11. *******************************
  12. *******************************/
  13. 'use strict';
  14. //using core modules
  15. var fs = require('fs') // file-system module
  16. , path = require('path'); // resolves relative paths
  17. var logger = require('../util/logger'); // for help menu
  18. //object to store input data
  19. var _args = { opts: {} };
  20. //some regex for pattern tests
  21. var valid_float=/^[0-9]*[.]?[0-9]+$/g
  22. , valid_file=/^.*[.]txt$/g;
  23. try { //read in console arguments
  24. for(var i=2; i < process.argv.length; i++){
  25. if(_args.opts.help) break;
  26. if(valid_float.test(process.argv[i])){
  27. _args.duration = parseFloat(process.argv[i]);//grab duration
  28. if(isNaN(_args.duration))
  29. throw new Error('[!] Error: Problem trying to parse duration arg');
  30. continue;
  31. }else if(valid_file.test(process.argv[i])){//grab txt file
  32. _args.filename = process.argv[i];
  33. _args.file_data = fs.readFileSync(path.resolve(_args.filename));
  34. //split buffer into a string array (1-line/element)
  35. _args.file_data = (_args.file_data.toString().split('\n'));
  36. continue;
  37. }else{
  38. switch(process.argv[i]){
  39. case '--color':
  40. case 'c':
  41. _args.opts.color = true;
  42. break;
  43. case '--help':
  44. case '-h':
  45. _args.opts.help = true;
  46. print_help();
  47. break;
  48. case '--fancy':
  49. case '-f':
  50. _args.opts.fancy = true;
  51. break;
  52. case '--debug':
  53. case '-d':
  54. _args.opts.debug = true;
  55. break;
  56. case '--verbose':
  57. case '-v':
  58. _args.opts.verbose = true;
  59. break;
  60. case '--export': // This is a debug param used for viewing topology data in an html
  61. case '-x': // webpage (html viewer source code not provided for project). (KISS)
  62. _args.opts.export_json = true;
  63. break;
  64. case '--inject':
  65. case '-i':
  66. _args.opts.injected_filename = process.argv[i+1];
  67. _args.opts.inject_data = require(path.resolve(_args.opts.injected_filename));
  68. i++;
  69. break;
  70. default:
  71. //handle mashed args ex: -vfi
  72. if(process.argv[i][0] === '-'){
  73. var injected = false;
  74. for(var j=1; j < process.argv[i].length; j++){
  75. switch(process.argv[i][j]){
  76. case 'c':
  77. _args.opts.color = true;
  78. break;
  79. case 'h':
  80. _args.opts.help = true;
  81. print_help();
  82. break;
  83. case 'f':
  84. _args.opts.fancy = true;
  85. break;
  86. case 'd':
  87. _args.opts.debug = true;
  88. break;
  89. case 'v':
  90. _args.opts.verbose = true;
  91. break;
  92. case 'x':
  93. _args.opts.export_json = true;
  94. break;
  95. case 'i':
  96. _args.opts.injected_filename = process.argv[i+1];
  97. _args.opts.inject_data = require(path.resolve(_args.opts.injected_filename));
  98. injected = true;
  99. break;
  100. }
  101. }
  102. if(injected) i++;
  103. }
  104. }
  105. }
  106. }
  107. } catch (err) {
  108. if (err.code === 'ENOENT')
  109. throw new Error('[!] Error: The file ' + _args.filename + ' was not found!');
  110. else
  111. throw err;
  112. }
  113. //check if minimum arguments were provided
  114. if((!_args.filename || !_args.duration) && !_args.opts.help){
  115. print_help();
  116. throw new Error('[!] Error: minimum arguments not provided!' + '\n' +
  117. '[*] Please pass the name of the topology file & duration to run in seconds \n');
  118. }
  119. function print_help () {
  120. var help_str = '\n Usage: node ./driver.js ./file.txt 99.9 \n\n' +
  121. ' where: \'file.txt\' is the topology file defining nodes & edges \n' +
  122. ' and \'99.9\' is the duration to run the simulation in seconds.\n\n' +
  123. ' Available Options: \n -------------------------------------------- \n' +
  124. ' -c, --color show fancy tables in color \n' +
  125. ' -d, --debug show debug (all) output \n' +
  126. ' -f, --fancy print pretty router tables \n' +
  127. ' -h, --help show this menu \n' +
  128. ' -i, --inject insert events defined in a json file, \n' +
  129. ' must be followed by a filename or path \n' +
  130. ' -v, --verbose show verbose (medium) output \n';
  131. setTimeout(function (){ logger.log(logger.LVL.MINIMAL, help_str); }, 100);
  132. }
  133. module.exports = {
  134. get_input: function () {
  135. return _args;
  136. }
  137. };