Skip to content

Commit

Permalink
Turn unrecognized flags into config options
Browse files Browse the repository at this point in the history
  • Loading branch information
aappleby committed Mar 4, 2024
1 parent 7fdff48 commit 7136283
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions hancho.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ async def flatten_async(x):
for y in x: result.extend(await flatten_async(y))
return result

# Tries to convert a string to an int, then a float, then gives up. Used for
# ingesting unrecognized flag values.
def maybe_as_number(x):
try:
return int(x)
except:
try:
return float(x)
except:
return x

################################################################################
# Simple logger that can do same-line log messages like Ninja

Expand Down Expand Up @@ -504,7 +515,15 @@ def needs_rerun(self):
parser.add_argument('-f', '--force', default=False, action="store_true", help='Force rebuild of everything')

(flags, unrecognized) = parser.parse_known_args()

this.config |= flags.__dict__

# Unrecognized flags become global config fields.
for span in unrecognized:
if m2 := re.match("-+([^=\s]+)(?:=(\S+))?", span):
this.config[m2.group(1)] = maybe_as_number(m2.group(2)) if m2.group(2) is not None else True

print(this.config)

result = main()
sys.exit(result)

0 comments on commit 7136283

Please sign in to comment.