Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 1 | # Copyright 2013, Big Switch Networks, Inc. |
| 2 | # |
| 3 | # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | # the following special exception: |
| 5 | # |
| 6 | # LOXI Exception |
| 7 | # |
| 8 | # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | # |
| 15 | # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | # |
| 17 | # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | # a copy of the EPL at: |
| 19 | # |
| 20 | # http://www.eclipse.org/legal/epl-v10.html |
| 21 | # |
| 22 | # Unless required by applicable law or agreed to in writing, software |
| 23 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | # EPL for the specific language governing permissions and limitations |
| 26 | # under the EPL. |
| 27 | |
| 28 | from optparse import OptionParser |
| 29 | |
| 30 | from loxi_globals import OFVersions |
| 31 | |
| 32 | ## |
| 33 | # Command line options |
| 34 | options = {} |
| 35 | |
| 36 | ## |
| 37 | # Command line arguments |
| 38 | args = [] |
| 39 | |
| 40 | ##@var config_default |
| 41 | # The default configuration dictionary for LOXI code generation |
| 42 | options_default = { |
| 43 | "lang" : "c", |
| 44 | "version-list" : "1.0 1.1 1.2 1.3", |
| 45 | "install-dir" : "loxi_output", |
| 46 | } |
| 47 | |
| 48 | def lang_normalize(lang): |
| 49 | """ |
| 50 | Normalize the representation of the language |
| 51 | """ |
| 52 | return lang.lower() |
| 53 | |
| 54 | def version_list_normalize(vlist): |
| 55 | """ |
| 56 | Normalize the version list and return as an array |
| 57 | """ |
| 58 | out_list = [] |
| 59 | # @fixme Map to OF version references |
| 60 | if vlist.find(',') > 0: |
| 61 | vlist = vlist.split(',') |
| 62 | else: |
| 63 | vlist = vlist.split() |
| 64 | vlist.sort() |
| 65 | for ver in vlist: |
| 66 | try: |
| 67 | out_list.append(OFVersions.from_string(ver)) |
| 68 | except KeyError: |
| 69 | sys.stderr.write("Bad version input, %s" % str(ver)) |
| 70 | sys.exit(1) |
| 71 | return out_list |
| 72 | |
| 73 | def process_commandline(default_vals=options_default): |
| 74 | """ |
| 75 | Set up the options dictionary |
| 76 | |
| 77 | @param cfg_dflt The default configuration dictionary |
| 78 | @return A pair (options, args) as per parser return |
| 79 | """ |
| 80 | global options |
| 81 | global args |
| 82 | global target_version_list |
| 83 | |
| 84 | parser = OptionParser(version="%prog 0.1") |
| 85 | |
| 86 | #@todo Add options via dictionary |
| 87 | parser.add_option("--list-files", action="store_true", default=False, |
| 88 | help="List output files generated") |
| 89 | parser.add_option("-l", "--lang", "--language", |
| 90 | default=default_vals["lang"], |
| 91 | help="Select the target language: c, python") |
| 92 | parser.add_option("-i", "--install-dir", |
| 93 | default=default_vals["install-dir"], |
| 94 | help="Directory to install generated files to (default %s)" % default_vals["install-dir"]) |
| 95 | parser.add_option("-v", "--verbose", |
| 96 | action="store_true", default=False, |
| 97 | help="Debug output") |
| 98 | |
| 99 | parser.add_option("-V", "--version-list", |
| 100 | default=default_vals["version-list"], |
| 101 | help="Specify the versions to target as 1.0 1.1 etc") |
| 102 | |
| 103 | (options, args) = parser.parse_args() |
| 104 | |
| 105 | options.lang = lang_normalize(options.lang) |
| 106 | target_version_list = version_list_normalize(options.version_list) |
| 107 | target_version_list.sort() |
| 108 | return (options, args, target_version_list) |