1# This Source Code Form is subject to the terms of the Mozilla Public 2# License, v. 2.0. If a copy of the MPL was not distributed with this 3# file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5from __future__ import absolute_import, print_function, unicode_literals 6 7import os 8from mozbuild.configure.options import Option 9 10 11class HelpFormatter(object): 12 def __init__(self, argv0): 13 self.intro = ['Usage: %s [options]' % os.path.basename(argv0)] 14 self.options = ['Options: [defaults in brackets after descriptions]'] 15 self.env = ['Environment variables:'] 16 17 def add(self, option): 18 assert isinstance(option, Option) 19 20 if option.possible_origins == ('implied',): 21 # Don't display help if our option can only be implied. 22 return 23 24 # TODO: improve formatting 25 target = self.options if option.name else self.env 26 opt = option.option 27 if option.choices: 28 opt += '={%s}' % ','.join(option.choices) 29 help = option.help or '' 30 if len(option.default): 31 if help: 32 help += ' ' 33 help += '[%s]' % ','.join(option.default) 34 35 if len(opt) > 24 or not help: 36 target.append(' %s' % opt) 37 if help: 38 target.append('%s%s' % (' ' * 28, help)) 39 else: 40 target.append(' %-24s %s' % (opt, help)) 41 42 def usage(self, out): 43 print('\n\n'.join('\n'.join(t) 44 for t in (self.intro, self.options, self.env)), 45 file=out) 46