{"id":22,"date":"2012-10-06T18:56:28","date_gmt":"2012-10-06T16:56:28","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=22"},"modified":"2012-10-09T09:21:08","modified_gmt":"2012-10-09T07:21:08","slug":"switches","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/06\/switches\/","title":{"rendered":"Switches"},"content":{"rendered":"<p>During the last weeks it happened often that I had to write some command line apps. The reason for this is that more and more things are running automatically in tools like FinalBuilder (I will write an article about this later).<\/p>\n<p>One thing that makes the command line app life easier is the encapsulation of the switches in a record (Current Delphi versions allow records with methods).<\/p>\n<pre class=\"lang:default decode:true\">type\r\n  TSwitches = record\r\n  strict private const\r\n    cHelpSwitch = '?';\r\n    cBackupCountSwitch = 'c';\r\n  public\r\n    class function BackupCount: Integer; static;\r\n    class function Help: Boolean; static;\r\n    class procedure PrintHelp; static;\r\n  end;\r\n\r\n...\r\n\r\nclass function TSwitches.BackupCount: Integer;\r\nvar\r\n  sCount: string;\r\nbegin\r\n  FindCmdLineSwitch(cBackupCountSwitch, sCount);\r\n  TryStrToInt(sCount, Result);\r\nend;\r\n\r\nclass function TSwitches.Help: Boolean;\r\nvar\r\n  sHelp: string;\r\nbegin\r\n  Result := (ParamCount = 0) or (FindCmdLineSwitch(cHelpSwitch, sHelp));\r\nend;\r\n\r\nclass procedure TSwitches.PrintHelp;\r\nbegin\r\n  Writeln(Format(SUsageSample, [TDBSwitches.cBackupFolderSwitch, TDBSwitches.cBackupCountSwitch]));\r\n\r\n  Writeln;\r\n  Writeln(SOptions);\r\n  Writeln;\r\n\r\n  Writeln(Format(SHelpSwitch, [TDBSwitches.cHelpSwitch]));\r\n  Writeln(Format(SBackupCountSwitch, [TDBSwitches.cBackupCountSwitch]));\r\nend;<\/pre>\n<p>This record can be used directly in the .dpr file.<\/p>\n<pre class=\"lang:default decode:true\">begin\r\n  if TDBSwitches.Help then\r\n    TDBSwitches.PrintHelp\r\n  else\r\n    TDBWork.DeleteBackup(TDBSwitches.BackupFolder, TDBSwitches.BackupCount);\r\nend.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>During the last weeks it happened often that I had to write some command line apps. The reason for this is that more and more things are running automatically in tools like FinalBuilder (I will write an article about this &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/06\/switches\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-tipsandtricks","tag-delphi"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":10,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":28,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions\/28"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}